2011-04-12 63 views
2

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

这里是我当前的代码

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

感谢

+0

这并不试图找出_why_你有提前小时粗野的解决方案。你在什么时区?你的服务器设置了什么时区? – 2011-04-12 14:41:42

回答

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"; 
?> 
2

你真的应该找出问题的根源并解决它。很可能你需要设置MySQL的时区以适合你自己的(或者服务器本身有错误的日期设置)。

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

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

使用SUBTIME()函数在你的查询,如果您不能更改服务器的时间,以反映相应的时间戳

0

SQL方法:

SELECT TIMESTAMPADD(the_timestamp, INTERVAL -4 HOURS) 

这将返回偏移时间