本文介绍了如何使用 PHP 将日期显示为 iso 8601 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 PHP 将我的 MySQL 数据库中的日期时间显示为 iso 8601 格式的字符串,但结果是错误的.

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.

2008 年 10 月 17 日出现为:1969-12-31T18:33:28-06:00,这显然不正确(年份应该是 2008 年而不是 1969 年)

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)

这是我正在使用的代码:

This is the code I'm using:

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

$post[3] 是我的 MySQL 数据库中的日期时间 (CURRENT_TIMESTAMP).

知道出了什么问题吗?

推荐答案

date 是 UNIX 时间戳,而不是数据库时间戳字符串.

The second argument of date is a UNIX timestamp, not a database timestamp string.

您需要使用 strtotime 转换您的数据库时间戳.

You need to convert your database timestamp with strtotime.

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

这篇关于如何使用 PHP 将日期显示为 iso 8601 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:10