天天看点

php 国际标准时间_时区-如何在PHP中获得格林威治标准时间?

这个问题启发了我,我写了一个替代函数将时间戳转换为所需的时区。

因此,CORRECT程序步骤记录,显示(转换)时间:

1) record timestamp with time()

2) If you need to get a different timezone, use:

2.1) Class based:

2.1.1) DateTime class + setTimezone

or

2.1.2) date_default_timezone_set() and then date()

2.1.1) is better and more flexible than 2.1.2)

Function:

function getLTime_class($ts, $uTZ, $format_str="Y.m.d H:i", $sTZ="America/Toronto"){

// $ts - timestamp recorded with time(); if have date => convert to timestamp: strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);

// $uTZ - TZ string (ex: 'America/Toronto'); convert timestamp to this TimeZone

// $sTZ - TZ string (ex: 'America/Toronto'); convert timestamp from this TimeZone

$TimeZone_server=new DateTimeZone($sTZ);

$date_obj=new DateTime("@$ts", $TimeZone_server);

$TimeZone_local=new DateTimeZone($uTZ);

$date_obj->setTimeZone($TimeZone_local);

return $date_obj->format($format_str);

}

Usage:

$date_input=$_POST['date_input']; //read from POST

$ts=strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);

$userTZ='America/Toronto'; //read from user params

$result=getLTime_class($ts, $userTZ, 'Y-m-d H:i:s');

2.2) Non-Class based:

Function:

//this function replaces the function with DateTime class. It's faster. $uTZoffset is integer.

function getLTime_nonclass($ts, $uTZoffset, $format_str="Y.m.d H:i"){

// $ts - timestamp recorded with time(); if have date => convert to timestamp: strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);

// $uTZoffset - TZ offset (integer) $uTZoffset must consider DTS (for ex: in summer $uTZoffset=-4; in winter $uTZoffset=-5)

$ts_offset=date('Z',$ts);

if ($uTZoffset) $add_offset=$ts_offset-date('Z'); //when not UTC

else $add_offset=0; //when UTC

return date($format_str,$ts-$ts_offset+$uTZoffset*3600+$add_offset);

}

Usage:

$date_input=$_POST['date_input']; //read from POST

$ts=strtotime($date_input); //for ex strtotime('2012-02-17 12:00:00'); => 1345219200 - same value for summer and winter (for toronto timezone);

$uTZoffset=-4; //read from user params. $uTZoffset - TZ offset (integer) $uTZoffset must consider DTS (for ex: in summer $uTZoffset=-4; in winter $uTZoffset=-5)

$result=getLTime_nonclass($ts, $uTZoffset, 'Y-m-d H:i:s');

结果:

$ date_input = 2012-08-17 12:00:00

Server date: summer (2013-08-26)

getLTime_class => $userTZ='America/Toronto' => 2012-08-17 12:00:00

getLTime_nonclass => $uTZoffset=-4 => 2012-08-17 12:00:00

getLTime_class => $userTZ='UTC' => 2012-08-17 16:00:00

getLTime_nonclass => $uTZoffset=0 => 2012-08-17 16:00:00

Server date: winter (2013-02-26)

getLTime_class => $userTZ='America/Toronto' => 2012-08-17 12:00:00

getLTime_nonclass => $uTZoffset=-5 => 2012-08-17 12:00:00

getLTime_class => $userTZ='UTC' => 2012-08-17 16:00:00

getLTime_nonclass => $uTZoffset=0 => 2012-08-17 16:00:00

$ date_input = 2012-02-17 12:00:00

Server date: summer (2013-08-26)

getLTime_class => $userTZ='America/Toronto' => 2012-02-17 12:00:00

getLTime_nonclass => $uTZoffset=-4 => 2012-02-17 12:00:00

getLTime_class => $userTZ='UTC' => 2012-02-17 17:00:00

getLTime_nonclass => $uTZoffset=0 => 2012-02-17 17:00:00

Server date: winter (2013-02-26)

getLTime_class => $userTZ='America/Toronto' => 2012-02-17 12:00:00

getLTime_nonclass => $uTZoffset=-5 => 2012-02-17 12:00:00

getLTime_class => $userTZ='UTC' => 2012-02-17 17:00:00

getLTime_nonclass => $uTZoffset=0 => 2012-02-17 17:00:00

我决定使用getLTime_nonclass希望比使用类更快地进行转换。我要问的是确认getLTime_nonclass是getLTime_class的有效替代品。请随时发表您的意见。 谢谢