我有一个年份(2002),我想将它转换为以下格式:
2002-00-00T00:00:00
我尝试了多个迭代,最后一个是这个:
$testdate = DateTime::createFromFormat(DateTime::ISO8601, date("c"))
echo date_format($testdate, '2002');
但是,即使我接近正确的时间,它似乎总是在结尾添加 +00:00 …
在PHP中,'c'格式总是附加时区偏移量。你无法避免这一点。但你可以从组件构建日期:
date('Y-m-d\TH:i:s', $testdate);
最好的方法是使用常量(PHP 5 >= 5.5.0, PHP 7)
date(DATE_ISO8601, $timeToChange);
Docs: http://php.net/manual/en/class.datetimeinterface.php#datetime.constants.types
DATE_ISO8601
also adds the +00:00
timezone bit OP is trying to avoid.Example: echo date(DATE_ISO8601, 1612308970); // prints "2021-02-02T23:36:10+0000"
- kaldimardate('Y-m-d\TH:i:s\Z', time() - date('Z'));
在IT技术中,有时候会遇到毫秒和最后几微秒的问题,这些毫秒和微秒通常是以4或8结尾的。将日期转换为ISO 8601格式可以通过以下方法之一实现:"date(DATE_ISO8601)",我曾经使用过这种解决方案。
// In this form it leaves the date as it is without taking the current date as a reference
$dt = new DateTime();
echo $dt->format('Y-m-d\TH:i:s.').substr($dt->format('u'),0,3).'Z';
// return-> 2020-05-14T13:35:55.191Z
// In this form it takes the reference of the current date
echo date('Y-m-d\TH:i:s'.substr((string)microtime(), 1, 4).'\Z');
return-> 2020-05-14T13:35:55.191Z
// Various examples:
$date_in = '2020-05-25 22:12 03.056';
$dt = new DateTime($date_in);
echo $dt->format('Y-m-d\TH:i:s.').substr($dt->format('u'),0,3).'Z';
// return-> 2020-05-25T22:12:03.056Z
//In this form it takes the reference of the current date
echo date('Y-m-d\TH:i:s'.substr((string)microtime(), 1, 4).'\Z',strtotime($date_in));
// return-> 2020-05-25T14:22:05.188Z