duanbin198788 2016-11-30 05: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 06: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部