如何在Php中将日期从一种格式转换为另一种格式

时间:2011-08-06 00:03:04

标签: php mysql datetime date datetime-format

如何将日期从“2011-08-01 04:01:45”转换为“星期一,2011年8月1日05:37:45 -0400”在Php

非常感谢...

3 个答案:

答案 0 :(得分:5)

$newDate = date('r', strtotime('2011-08-01 04:01:45'));

答案 1 :(得分:1)

使用php中的strtotime函数解析日期字符串,然后使用日期函数以任何您喜欢的格式打印它。

<?php

$orig = "2011-08-01 04:01:45";

$timestamp = strtotime($orig);

echo date("D, d M Y H:i:s O",$timestamp);
?>

答案 2 :(得分:0)

我知道这不会解决您的问题,但使用日期和信息的一种非常快速的方法是使用unix时间戳来存储时间。这是从时间(1970-1秒)开始计算的秒的整数值。

它允许您使用以下内容以更简单的方式格式化日期:

<?php date('d-m-Y', $unixTimeStamp); ?>

这种方法在比较时间方面也快得多。