10

我有一年(2002 年),我正在尝试将其转换为以下格式:

2002-00-00T00:00:00

我尝试了各种迭代,最后一个是这样的:

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

但是,即使我接近,它似乎总是在它的末尾添加 +00:00 ......

4

4 回答 4

17

PHP 中的 'c' 格式总是附加时区偏移量。你无法避免这一点。但是您可以从组件中自己构建日期:

date('Y-m-d\TH:i:s', $testdate);
于 2011-09-09T22:12:52.323 回答
5

最好的方法是使用常量(PHP 5 >= 5.5.0, PHP 7)

date(DATE_ISO8601, $timeToChange);

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

于 2018-11-12T10:22:18.973 回答
3

问题多次出现在4 或 8决赛中的毫秒和最终微秒。要将DATE 转换为 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

以前发表:https ://stackoverflow.com/a/61796705/5898408

于 2020-05-14T11:55:33.683 回答
1
date('Y-m-d\TH:i:s\Z', time() - date('Z'));
于 2018-02-27T06:24:29.667 回答