简体   繁体   English

如何使用 PHP 将日期显示为 iso 8601 格式

[英]How to display a date as iso 8601 format with PHP

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 数据库中$post[3] is the datetime (CURRENT_TIMESTAMP)

Any ideas what's going wrong?任何想法出了什么问题?

The second argument of date is a UNIX timestamp, not a database timestamp string. date的第二个参数是 UNIX 时间戳,而不是数据库时间戳字符串。

You need to convert your database timestamp with strtotime .您需要使用strtotime转换数据库时间戳。

<?= date("c", strtotime($post[3])) ?>

Using the DateTime class available 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');

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 'Ymd\\TH:i:sP' as an alternative to 'c' for your format. 1) 您也可以使用'Ymd\\TH:i:sP'作为'c'的替代格式。

2) The default time zone of your input is the time zone of your server. 2)您输入的默认时区是您的服务器的时区。 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 :但是,这也会影响您的输出:

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

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';
}

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));
}

The problem many times occurs with the milliseconds and final microseconds that many times are in 4 or 8 finals.问题多次出现在4 或 8决赛中的毫秒和最终微秒。 To convert the DATE to ISO 8601 "date(DATE_ISO8601)" these are one of the solutions that works for me:要将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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM