如何使用 PHP 将日期显示为 iso 8601 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/903203/
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
How to display a date as iso 8601 format with PHP
提问by Matthew James Taylor
I'm trying to display a datetime from my MySQL database as an iso 8601 formated string with PHP but it's coming out wrong.
我试图用 PHP 将我的 MySQL 数据库中的日期时间显示为 iso 8601 格式的字符串,但结果是错误的。
17 Oct 2008 is coming out as: 1969-12-31T18:33:28-06:00 which is clearly not correct (the year should be 2008 not 1969)
2008 年 10 月 17 日是: 1969-12-31T18:33:28-06:00 这显然是不正确的(年份应该是 2008 年而不是 1969 年)
This is the code I'm using:
这是我正在使用的代码:
<?= date("c", $post[3]) ?>
$post[3] is the datetime (CURRENT_TIMESTAMP)from my MySQL database.
$post[3] is the datetime (CURRENT_TIMESTAMP)从我的 MySQL 数据库。
Any ideas what's going wrong?
任何想法出了什么问题?
回答by Paolo Bergantino
回答by John Conde
Using the DateTime classavailable in PHP version 5.2 it would be done like this:
使用PHP 5.2 版中提供的DateTime 类,可以这样完成:
$datetime = new DateTime('17 Oct 2008');
echo $datetime->format('c');
As of PHP 5.4 you can do this as a one-liner:
从 PHP 5.4 开始,您可以单行执行此操作:
echo (new DateTime('17 Oct 2008'))->format('c');
回答by John Slegers
Procedural style :
程序风格:
echo date_format(date_create('17 Oct 2008'), 'c');
// Output : 2008-10-17T00:00:00+02:00
Object oriented style :
面向对象风格:
$formatteddate = new DateTime('17 Oct 2008');
echo $datetime->format('c');
// Output : 2008-10-17T00:00:00+02:00
Hybrid 1 :
混合1:
echo date_format(new DateTime('17 Oct 2008'), 'c');
// Output : 2008-10-17T00:00:00+02:00
Hybrid 2 :
混合2:
echo date_create('17 Oct 2008')->format('c');
// Output : 2008-10-17T00:00:00+02:00
Notes :
注意事项:
1) You could also use 'Y-m-d\TH:i:sP'as an alternative to 'c'for your format.
1) 您也可以使用'Y-m-d\TH:i:sP'替代'c'您的格式。
2) The default time zone of your input is the time zone of your server. If you want the input to be for a different time zone, you need to set your time zone explicitly. This will also impact your output, however :
2) 您输入的默认时区是您服务器的时区。如果您希望输入用于不同的时区,则需要明确设置您的时区。但是,这也会影响您的输出:
echo date_format(date_create('17 Oct 2008 +0800'), 'c');
// Output : 2008-10-17T00:00:00+08:00
3) If you want the output to be for a time zone different from that of your input, you can set your time zone explicitly :
3)如果您希望输出的时区与输入的时区不同,则可以明确设置时区:
echo date_format(date_create('17 Oct 2008')->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2008-10-16T18:00:00-04:00
回答by Newmania
For pre PHP 5:
对于 PHP 5 之前的版本:
function iso8601($time=false) {
if(!$time) $time=time();
return date("Y-m-d", $time) . 'T' . date("H:i:s", $time) .'+00:00';
}
回答by Guillaume
Here is the good function for pre PHP 5: I added GMT difference at the end, it's not hardcoded.
这是 PHP 5 之前的好功能:我在最后添加了 GMT 差异,它不是硬编码的。
function iso8601($time=false) {
if ($time === false) $time = time();
$date = date('Y-m-d\TH:i:sO', $time);
return (substr($date, 0, strlen($date)-2).':'.substr($date, -2));
}
回答by Clary
The problem many times occurs with the milliseconds and final microseconds that many times are in 4 or 8finals. To convert the DATE to ISO 8601"date(DATE_ISO8601)"these are one of the solutions that works for me:
问题多次出现在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

