如何在PHP中增加MySQL时间戳?

时间:2011-04-12 14:32:06

标签: php mysql datetime

所以我有一个MYSQL时间戳,它产生的值比它们应该提前4小时。我如何从MySQL时间戳中减去4小时并显示出来?

这是我当前的代码

$mdate = date('F j, Y, g:i a',strtotime( $bm->date_added)); 
//$bm->dated_added returns the times stamp//
echo $mdate;

由于

4 个答案:

答案 0 :(得分:4)

当提供第二个参数时,strtotime()将根据此日期执行计算:

$mdate = date('F j, Y, g:i a', strtotime('-4 hours', $bm->date_added)); 

strtotime()可以根据这些修饰符执行多项计算。 manual

中的示例
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

答案 1 :(得分:2)

你应该确定根本问题并修复它。您很可能需要设置MySQL的时区以适合您自己的时区(或者服务器本身设置了错误的日期)。

那就是说,你可以从strtotime结果中减去14,400秒。

$mdate = date('F j, Y, g:i a',strtotime($bm->date_added) - 14400); 

答案 2 :(得分:1)

如果无法更改服务器时间以反映适当的时间戳,请在查询中使用SUBTIME()函数

答案 3 :(得分:0)

SQL方法:

SELECT TIMESTAMPADD(the_timestamp, INTERVAL -4 HOURS)

这将返回偏移时间