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 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao