php 如何在php中将ISO8601转换为日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22376415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:52:36  来源:igfitidea点击:

How to convert ISO8601 to Date format in php

phpmysqlsqldatedatetime

提问by Neel

How to convert this (in ISO8601 format):2014-03-13T09:05:50.240Z

如何转换(以 ISO8601 格式):2014-03-13T09:05:50.240Z

To this (in MySQL DATE format):2014-03-13

对此(在 MySQL DATE 格式中):2014-03-13

in php?

在 php 中?

回答by ThatMSG

try this

尝试这个

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

The complete date function documentation can be found here: http://php.net/manual/en/function.date.php

完整的日期函数文档可以在这里找到:http: //php.net/manual/en/function.date.php

The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.

PHP 函数“strtotime”不执行任何其他操作,然后将您的时间字符串转换为 unix 时间戳。

Hope I could help :)

希望我能帮上忙:)

P.s.: Just in case strtotime will return 0 try using this:

Ps:以防万一 strtotime 将返回 0 尝试使用这个:

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime(substr($date,0,10)));

回答by Faisal

Since PHP 5.2.0you can do it using OOPand DateTime()as well (of course if you prefer OOP):

由于PHP5.2.0可以用做OOPDateTime()也(当然如果你喜欢OOP):

$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d');    // MySQL datetime format

回答by Wiimm

There is no reason to use the inefficient time functions. The most efficient way is to simply extract the first 10 characters:

没有理由使用低效的时间函数。最有效的方法是简单地提取前 10 个字符:

substr($date,0,10)

回答by Mitul Shah

Simply convert datetime description into a Unix timestamp using with strtotimeand then five format using Date Formats

只需使用strtotime将日期时间描述转换为 Unix 时间戳,然后使用日期格式将五种格式转换为

Try it will surely work for you.

尝试它肯定对你有用。

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));