开发者

Change from Local Time to GMT for unix format time

Using PHP/mysql I implemented a form for saving in the db a time in unix f开发者_Go百科ormat using the function mktime. The view form displays the time in a readable format using date function. Only now I discovered that I was using local time and not GMT (UTC) time as requested. The changes are quite easy in the sw changing mktime for gmmktime and date for gmdate. But the question is if there is a way to convert the time already in the DB (in unix format) from local time to gmt time. Thanks


You could use the function gmdate(). Have a look at the Manual.


Here's a function I wrote to do timezone conversions. Should be pretty self-explanatory:

function switch_timezone($format, $time = null, 
    $to = "America/Los_Angeles", $from = "America/Los_Angeles")
{
    if ($time == null) $time = time();

    $from_tz = new DateTimeZone($from);
    $to_tz = new DateTimeZone($to);

    if (is_int($time)) $time = '@' . $time;

    $dt = date_create($time, $from_tz);

    if ($dt)
    {
        $dt->setTimezone($to_tz);
        return $dt->format($format);
    }

    return date($format, $time);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜