开发者

get an hour wrong when using UNIX_TIMESTAMP and time()

开发者 https://www.devze.com 2023-02-01 09:12 出处:网络
I have开发者_高级运维 set my mysql db: SET time_zone = \'America/Los_Angeles\'; and in php date_default_timezone_set(\'America/Los_Angeles\');

I have开发者_高级运维 set my mysql db: SET time_zone = 'America/Los_Angeles'; and in php

date_default_timezone_set('America/Los_Angeles');   

but when I add

$time = strtotime ("+2 minutes");

and

$time2 = gmdate("Y-m-d H:i:s", $time);  

to the db i get wrong an hour wrong when I get it from mysql like this:

        $query ="select UNIX_TIMESTAMP("time"), time2 from tbl; 
        if ($result = $conn->query($query)) {
            while ($row = $result->fetch_assoc()) {
                $unix_time = $row["UNIX_TIMESTAMP("time")"];
                $unix_time2 = $row["time2"];
            }
            $result->close();
        }       ,

echo gmdate("Y-m-d H:i:s", $unix_time)." | ".gmdate("Y-m-d H:i:s", $unix_time2)." "; 

displays:

2010-12-26 01:06:48 | 2010-12-26 02:06:24


unix_timestamp() will retrieve your local timestamp, which is currently in daylight savings mode. gmdate() works off Greenwhich Mean Time, aka UTC, which has no DST, so you'll get the 1 hour difference. Use date() instead of gmdate() to work with DST-awareness.

0

精彩评论

暂无评论...
验证码 换一张
取 消