dora0817 2019-07-24 02:32
浏览 11
已采纳

无法在PHP中格式化UTC日期

With a php app, I have a date like this '2019-07-22T04:44:43.843710438Z' and I can't figure out how to convert it to a date like this 7/22/2019 3:11 PM. strtotime just returns false, I tried a few time zones and nothing works. Here is my scratch pad of stuff I've been trying

 $timestamp = '2019-07-22T04:44:43.843710438Z';
//$reset = date_default_timezone_get();
//date_default_timezone_set('America/New_York');
//$stamp = strtotime($timestamp);
//date_default_timezone_set($reset);

$result = date_parse_from_format("j F Y G:i", $timestamp);
  • 写回答

2条回答 默认 最新

  • dongnius85154 2019-07-24 02:42
    关注

    You'll have a much easier time (couldn't resist) working with the DateTime class (https://www.php.net/manual/en/class.datetime.php).

    You're running into issues because the value you have isn't a timestamp so you can't use it like one.

    The original time value uses nanosecond precision (9dp) which isn't supported by PHP. In order to use that time string with DateTime, you'll first have to truncate it to microseconds (6dp). It then has a method that will return your value in the desired format (https://www.php.net/manual/en/function.date.php).

    $time = '2019-07-22T04:44:43.843710438Z';
    $time_micro_precision = substr($time, 0, -4) . 'Z'; // drop the last three digits to move from nanoseconds to microseconds.
    
    $datetime = new DateTime($time_micro_precision);
    echo $datetime->format('j/n/Y g:i A'); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化