本地时区转化为他国时区时间

数据库中存入数据时间时采用的是中国时区,现在要显示泰国时间,故使用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 );


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值