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 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输