PHP日期偏移量


php date offset

这是我的问题,我有一个时间戳来自php,服务器时间是3小时从我的。我把时间偏移了三个小时,但很快意识到,在凌晨2点,时间戳的日期部分读取的是前一天的日期(日和月),直到凌晨3点才会改变。这是一个问题,因为日期很重要,我需要它是准确的。我已经尝试了时区的变化,但似乎不能得到它的工作。我住在俄亥俄州,所以这是我需要的时间,而服务器所在的时区晚了三个小时。所以有两件事可以帮助我,一个是改变时区,或者把一天的时间偏移3个小时,而不仅仅是时间。下面是我当前的代码:

 $timechange = mktime(date("g")+3, date("i"), 0, date("m"), date("d"), date("y"));
 $date = date("D, d M Y g:i",$timechange);
$now = new DateTime('now', new DateTimeZone('America/Ohio')); // whatever your TZ's name happens to be
$now->setTimeZone('America/ServerTZ'); // reset to your server's TZ
$datestr = $now->format('D, d M Y g:i'); // get TZ's time as a nice string

通过执行setTimeZone,您将影响函数的OUTPUT—内部时间戳是不变的。

与其自己调整时间,不如事先设置好时区。

见http://php.net/manual/en/function.date-default-timezone-set.php