开发者

Convert GMT to Local time

Edit: This function does work in PHP, it isn't working for me within the CakePHP framework which I didn't think relevant when originally posting.

This function takes a string formatted date/time and a local timezone (e.g. 'America/New_York'). It supposed to return time converted to the local timezone. Currently, it does not change.

I pass it: '2011-01-16 04:57:00', 'America/New_York' and I get back the same time I pass in.

function getLocalfromGMT($datetime_gmt, $local_timezone){
        $ts_gmt = strtotime($datetime_gmt.' GMT');
        $tz = getenv('TZ');
        // next two lines seem to do no conversion
        putenv("TZ=$local_timezone");
        $ret = date('Y-m-j H:i:s',$ts_gmt);
        putenv("TZ=$tz");
        return $ret;
    }

I'开发者_StackOverflow中文版ve seen the references to the new methods for default_timezone_get/set. I'm not currently interested in using that method because I'd like this code to work with older versions of PHP.


Apparently, in CakePHP, if you're using date_default_timezone_set() in your config file, which we are, the TZ environment variable setting method does not work. So, the new version, which seems to work perfectly is:

function __getTimezone(){
        if(function_exists('date_default_timezone_get')){
            return date_default_timezone_get();
        }else{
            return getenv('TZ');
        }
    }

    function __setTimezone($tz){
        if(function_exists('date_default_timezone_set')){
            date_default_timezone_set($tz);
        }else{
            putenv('TZ='.$tz);
        }
    }

    // pass datetime_utc in a standard format that strtotime() will accept
    // pass local_timezone as a string like "America/New_York"
    // Local time is returned in YYYY-MM-DD HH:MM:SS format
    function getLocalfromUTC($datetime_utc, $local_timezone){
        $ts_utc = strtotime($datetime_utc.' GMT');
        $tz = $this->__getTimezone();
        $this->__setTimezone($local_timezone);
        $ret = date('Y-m-j H:i:s',$ts_utc);
        $this->__setTimezone($tz);
        return $ret;
    }


how about this

<?php


        // I am using the convention (assumption) of "07/04/2004 14:45"
        $processdate = "07/04/2004 14:45";


        // gmttolocal is a function
        // i am passing it 2 parameters:
        // 1)the date in the above format and
        // 2)time difference as a number; -5 in our case (GMT to CDT)
        echo gmttolocal($processdate,-5);



function gmttolocal($mydate,$mydifference)  
{
        // trying to seperate date and time
        $datetime = explode(" ",$mydate);

        // trying to seperate different elements in a date
        $dateexplode = explode("/",$datetime[0]);

        // trying to seperate different elements in time
        $timeexplode = explode(":",$datetime[1]);


// getting the unix datetime stamp
$unixdatetime = mktime($timeexplode[0]+$mydifference,$timeexplode[1],0,$dateexplode[0],$dateexplode[1],$dateexplode[2]);

        // return the local date
        return date("m/d/Y H:i",$unixdatetime));
}


?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜