douwo3665 2017-02-10 17:44
浏览 51
已采纳

PHP未知日期时间格式

I have the DateTime format: 2016-04-06T15:17:42.97074Z that I obtained from v5 on the twitch api.

I need to turn this into the unix timestamp for comparison using PHP.

This is currently how I'm attempting the conversion into a DateTime object: $dateTime = DateTime::createFromFormat(DateTime::ISO8601, $json["created_at"]);, I have also tried using DateTime::W3C but to no avail.

$json["created_at"] definitely contains the time to convert as echo $json["created_at"] returns the time.

  • 写回答

1条回答 默认 最新

  • dongyou8701 2017-02-10 17:50
    关注

    createFromFormat rejects the fractional seconds when asked to parse ISO8601, but you can simply construct a DateTime using that string successfully, e.g.

    $str='2016-04-06T15:17:42.97074Z';
    //this will work... 
    $dt1=new \DateTime($str);
    //but this won't...
    $dt2=\DateTime::createFromFormat(\DateTime::ATOM, $str);
    
    var_dump($dt1);
    var_dump($dt2);
    

    This will output the following in php 5.6.0 - 5.6.30, hhvm-3.15.4 - 3.17.1, 7.0.0 - 7.1.1

    object(DateTime)#1 (3) {
      ["date"]=>
      string(26) "2016-04-06 15:17:42.970740"
      ["timezone_type"]=>
      int(2)
      ["timezone"]=>
      string(1) "Z"
    }
    bool(false)
    

    See also PHP DateTime::createFromFormat doesn't parse ISO 8601 date time

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?