我如何将日期转换成这种格式,2013-08-09T19:08:28Z


How can I get a date into this format, 2013-08-09T19:08:28Z?

在我需要从中获取一些信息的表中,created_at和updated_at列被错误地定义为VARCHAR,而不是时间戳。因此,我需要在查询中将日期作为字符串传递。

我需要获得以下格式的日期:

2013-08-09T19:08:28Z

我更喜欢在PHP中使用DateTime类。

您需要使用c限定符

$dt = new DateTime();
echo $dt->format('c');

用文字Z:字面指定格式字符串

$dt = new DateTime('now', new DateTimeZone('UTC'));
echo $dt->format('Y-m-d'TH:i:s'Z');