I need to work out the difference in hours between two timezones and I am running into some issues when the timezone that is ahead moves to the next day.
我需要弄清楚两个时区之间的小时差异,当前面的时区移动到第二天时,我遇到了一些问题。
Example:
//Let's say it is 11pm 23:00 in LA
$local_tz = new DateTimeZone('America/Los_Angeles');
$local = new DateTime('now', $local_tz);
$local_hour = $local->format('H');
//NY is 3 hours ahead, so it is 2am, 02:00
$user_tz = new DateTimeZone('America/New_York');
$user = new DateTime('now', $user_tz);
$user_hour = $user->format('H');
Following the example in this question (Calculate hours between dates in different time zones) I get an incorrect result:
按照此问题中的示例(计算不同时区中日期之间的小时数),我得到的结果不正确:
$diff = $user_tz->getOffset($local);
error_log('$diff '.gmdate("H:i:s", $diff)); //outputs 20:00:00
If it was 4pm in LA, therefore 7pm in NY then it is easy:
如果是在洛杉矶的下午4点,那么在纽约的晚上7点那么很容易:
$time_diff = ($user_h - $local_h); //$time_diff = 3;
But when NY moves to the next day I again get incorrect results:
但是当纽约搬到第二天时,我又得到了不正确的结果:
$time_diff = ($user_h - $local_h); //$time_diff = -21;
So how can I account for another timezone which has moved to a new day?
那么我怎样才能解释另一个已经搬到新的一天的时区呢?
2 个解决方案
#1
0
I managed to find a solution to this, the DateInterval class kept throwing out 21 for me aswell, although when I outputted the object I could see the hour as 3.
我设法找到了一个解决方案,DateInterval类不断为我抛出21,虽然当我输出对象时我可以看到小时为3。
Since DateTime does the comparison with epoch and ignores timezone, I had to create a new DateTime for both times and THEN run the comparison.
由于DateTime与epoch进行比较并忽略时区,因此我必须为两个时间创建一个新的DateTime,然后运行比较。
//Let's say it is 11pm 23:00 in LA
$local_tz = new DateTimeZone('America/Los_Angeles');
$local = new DateTime('now', $local_tz);
//NY is 3 hours ahead, so it is 2am, 02:00
$user_tz = new DateTimeZone('America/New_York');
$user = new DateTime('now', $user_tz);
$usersTime = new DateTime($user->format('Y-m-d H:i:s'));
$localsTime = new DateTime($local->format('Y-m-d H:i:s'));
$interval = $usersTime->diff($localsTime);
print_r($interval->h); //outputs 3
Running tests with $user->modify("-4 hours"); $local->modify("-4 hours");
returns 3. (which puts one of those just before midnight the previous day)
使用$ user-> modify运行测试(“ - 4小时”); $ local-> modify(“ - 4小时”);返回3.(将其中一个放在前一天的午夜之前)
#2
0
You can get difference in hours between two time zones using timezone offset try below code:
您可以使用时区偏移在两个时区之间获得小时差异,请尝试以下代码:
$local_tz = new DateTimeZone('America/Los_Angeles');
$local = new DateTime('now', $local_tz);
//NY is 3 hours ahead, so it is 2am, 02:00
$user_tz = new DateTimeZone('America/New_York');
$user = new DateTime('now', $user_tz);
$local_offset = $local->getOffset() / 3600;
$user_offset = $user->getOffset() / 3600;
$diff = $user_offset - $local_offset;
print_r($diff); //outputs 3
#1
0
I managed to find a solution to this, the DateInterval class kept throwing out 21 for me aswell, although when I outputted the object I could see the hour as 3.
我设法找到了一个解决方案,DateInterval类不断为我抛出21,虽然当我输出对象时我可以看到小时为3。
Since DateTime does the comparison with epoch and ignores timezone, I had to create a new DateTime for both times and THEN run the comparison.
由于DateTime与epoch进行比较并忽略时区,因此我必须为两个时间创建一个新的DateTime,然后运行比较。
//Let's say it is 11pm 23:00 in LA
$local_tz = new DateTimeZone('America/Los_Angeles');
$local = new DateTime('now', $local_tz);
//NY is 3 hours ahead, so it is 2am, 02:00
$user_tz = new DateTimeZone('America/New_York');
$user = new DateTime('now', $user_tz);
$usersTime = new DateTime($user->format('Y-m-d H:i:s'));
$localsTime = new DateTime($local->format('Y-m-d H:i:s'));
$interval = $usersTime->diff($localsTime);
print_r($interval->h); //outputs 3
Running tests with $user->modify("-4 hours"); $local->modify("-4 hours");
returns 3. (which puts one of those just before midnight the previous day)
使用$ user-> modify运行测试(“ - 4小时”); $ local-> modify(“ - 4小时”);返回3.(将其中一个放在前一天的午夜之前)
#2
0
You can get difference in hours between two time zones using timezone offset try below code:
您可以使用时区偏移在两个时区之间获得小时差异,请尝试以下代码:
$local_tz = new DateTimeZone('America/Los_Angeles');
$local = new DateTime('now', $local_tz);
//NY is 3 hours ahead, so it is 2am, 02:00
$user_tz = new DateTimeZone('America/New_York');
$user = new DateTime('now', $user_tz);
$local_offset = $local->getOffset() / 3600;
$user_offset = $user->getOffset() / 3600;
$diff = $user_offset - $local_offset;
print_r($diff); //outputs 3