duanhuhong5255 2013-12-10 03:55
浏览 174
已采纳

PHP DateTime :: createFromFormat不使用带时区的时间格式

I want to check if submitted datetime from user is the same as my datetime format with

DateTime::createFromFormat($format, $date)

It was working fine until I changed my time format from Y-m-d H:i:s to Y-m-d\TH:i:s.ZP I want to get timedate like this 2011-11-09T17:11:57.430+05:00 What format should I give to my function? Is the format I used wrong?

  • 写回答

2条回答 默认 最新

  • douao8204 2013-12-10 11:54
    关注

    You are almost there. You have used 'Z' where you should have a 'u' for microseconds, so your code should look something like this:-

    $dateStr = '2011-11-09T17:11:57.430+05:00';
    $date = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateStr);
    var_dump($date);
    

    Output:-

    object(DateTime)[1]
      public 'date' => string '2011-11-09 17:11:57' (length=19)
      public 'timezone_type' => int 1
      public 'timezone' => string '+05:00' (length=6)
    

    The format strings accepted by DateTime::createFromFormat() are the same as those accepted by date().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?