duanbin198788 2016-11-30 13:40
浏览 13
已采纳

将字符串的格式模式更正为DateTime

I have a string looking like this 2016-11-29T14:04:07+00:00, can anyone hint me what would be the correct format to in order to create a DateTime object using createFromFormat I have already tried some options but none of them seem to work until now.

Thanks in advance for the help.

  • 写回答

2条回答 默认 最新

  • dsykx64220 2016-11-30 14:03
    关注

    DateTime has predefined constants for certain formats. Your date follows the ISO 8601 standard and its equivalent DateTime constant is called ATOM. So try:

    $date = DateTime::createFromFormat(DateTime::ATOM, '2016-11-29T14:04:07+00:00');
    

    Note that there is another constant called ISO8601 which is not compatible as stated in the documentation:

    This format is not compatible with ISO-8601, but is left this way for backward compatibility reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.

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

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题