数据库中存入数据时间时采用的是中国时区,现在要显示泰国时间,故使用php的timezone系列函数和类解决这个问题。
先计算2个时区相差的秒数,然后用存储时间加上差值进行调整后格式化显示出泰国时间。代码如下:
function offset($remote, $local = NULL, $now = NULL){
if ($local === NULL)
{
// Use the default timezone
$local = date_default_timezone_get();
}
if (is_int($now))
{
// Convert the timestamp into a string
$now = date(DateTime::RFC2822, $now);
}
// Create timezone objects
$zone_remote = new DateTimeZone($remote);
$zone_local = new DateTimeZone($local);
// Create date objects from timezones
$time_remote = new DateTime($now, $zone_remote);
$time_local = new DateTime($now, $zone_local);
// Find the offset
$offset = $zone_remote->getOffset($time_remote) - $zone_local->getOffset($time_local);
return $offset;
}
$now = '2012-09-14 19:12:31';
$offset = offset( 'Asia/Bangkok', 'Asia/Shanghai', $now );
echo $now,' : ',date( 'Y-m-d H:i:s', strtotime( $now )+$offset );