PHP - format date ISO8601?

北战南征 提交于 2019-12-21 14:04:21

问题


I have a year (2002) and I'm trying to get it into the following format:

2002-00-00T00:00:00

I tried various iterations, the last of which was this:

$testdate = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
echo date_format($testdate, '2002'); 

But, even if I come close, it always seems to add +00:00 to the end of it...


回答1:


The 'c' format in PHP always appends the timezone offset. You can't avoid that. But you can build the date yourself from components:

date('Y-m-d\TH:i:s', $testdate);



回答2:


Best way is to use constants (PHP 5 >= 5.5.0, PHP 7)

date(DATE_ISO8601, $timeToChange);

Docs: http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types




回答3:


date('Y-m-d\TH:i:s\Z', time() - date('Z'));


来源:https://stackoverflow.com/questions/7367953/php-format-date-iso8601

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!