我正在尝试使用PHP将来自MySQL数据库的日期时间显示为ISO 8601格式的字符串,但结果不正确。
2008年10月17日显示为1969-12-31T18:33:28-06:00,这显然是不正确的(年份应该是2008而不是1969)
这是我正在使用的代码:
<?= date("c", $post[3]) ?>
$post[3]
是来自我的 MySQL 数据库的日期时间(CURRENT_TIMESTAMP)。
有任何想法出了什么问题吗?
我正在尝试使用PHP将来自MySQL数据库的日期时间显示为ISO 8601格式的字符串,但结果不正确。
2008年10月17日显示为1969-12-31T18:33:28-06:00,这显然是不正确的(年份应该是2008而不是1969)
这是我正在使用的代码:
<?= date("c", $post[3]) ?>
$post[3]
是来自我的 MySQL 数据库的日期时间(CURRENT_TIMESTAMP)。
有任何想法出了什么问题吗?
使用 PHP 5.2 版本中提供的 DateTime 类,可以按照以下方式完成:
$datetime = new DateTime('17 Oct 2008');
echo $datetime->format('c');
从PHP 5.4开始,你可以将这个操作变成单行代码:
echo (new DateTime('17 Oct 2008'))->format('c');
echo date_format(date_create('17 Oct 2008'), 'c');
// Output : 2008-10-17T00:00:00+02:00
$formatteddate = new DateTime('17 Oct 2008');
echo $datetime->format('c');
// Output : 2008-10-17T00:00:00+02:00
echo date_format(new DateTime('17 Oct 2008'), 'c');
// Output : 2008-10-17T00:00:00+02:00
echo date_create('17 Oct 2008')->format('c');
// Output : 2008-10-17T00:00:00+02:00
1)您还可以使用'Y-m-d\TH:i:sP'
作为您的格式的替代方式,而不是'c'
。
2)输入的默认时区是您服务器的时区。 如果您需要输入来自其他时区,则需要显式设置您的时区。 然而,这也会影响您的输出:
echo date_format(date_create('17 Oct 2008 +0800'), 'c');
// Output : 2008-10-17T00:00:00+08:00
3) 如果您想要的输出时区与您输入的时区不同,您可以明确地设置自己的时区:
echo date_format(date_create('17 Oct 2008')->setTimezone(new DateTimeZone('America/New_York')), 'c');
// Output : 2008-10-16T18:00:00-04:00
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));
}
对于 PHP 5 之前的版本:
function iso8601($time=false) {
if(!$time) $time=time();
return date("Y-m-d", $time) . 'T' . date("H:i:s", $time) .'+00:00';
}
许多时候,问题出现在毫秒和最终微秒上,它们通常是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
ISO8601DateFormatter
和你的PHP服务器日期格式之间找到解决方案的人 - 上面的工作代码是 $dt->format('Y-m-d\TH:i:s.').substr($dt->format('u'),0,3).'Z';
。谢谢你,伙计! - Sherbieny