我一直在使用DATETIME列将用户消息存储在我的数据库中。我需要将消息的发送时间显示给不同国家的用户,因为数据库使用GMT + 0我需要将检索到的时间调整到用户的时区。获取时区GMT时间?
当前用户的时区已设置为date_default_timezone_set()如何获取GMT +时间?
编辑
继的想法@doniyor这是我做过什么:
// Get server hour
$localtime = localtime();
$serverHour = $localtime[2]; # The server's timezone is used by default before defining one
// Set timezone
date_default_timezone_set('user's timezone goes here');
// Get user hour
$localtime = localtime(); # Now localtime gives the user's time since timezone has been changed
$userHour = $localtime[2];
$timeAdjustment = $userHour - $serverHour < -12 ? 24 + $userHour - $serverHour : $userHour - $serverHour; // Calculate hours to add to rows got from db to match user's time
// Example of a row adjusted to match user's time
$adjustTime = ($userHour - $serverHour < -12 ? 24 + $userHour - $serverHour : ($userHour - $serverHour > 12 ? $userHour - $serverHour - 24 : $userHour - $serverHour)*60*60; // strtotime is in seconds so $timeAdjustment needs *60*60 to convert to seconds too
我测试过这在PHP5 + Apache的,但可能的结果取决于服务器的配置。
尝试使用Unix的时间戳,然后将其转换为日期 – Andrew
它使用的只是网页建立日期时间,所以可能如果我改变时间戳我会得到很多错误 – lisovaccaro