以编程方式确定使用UTC时间在一天/时间的偏移量

时间:2015-04-15 00:02:41

标签: php datetime utc gmt

我有一个每小时开火的cron工作。在我的数据库中,我有包含UTC偏移的行。我正在尝试以编程方式执行以下操作。

function returnThisHourOffset($timeofday){
    //using the current UTC hour gmdate('G'), determine at what offset from the UTC the $timeofday currently exists (**and which day**).
}

我已经写出了每个UTC午夜和凌晨5点会发生什么。

    UTC 23:00:00, At offset +1 is 12AM the next UTC day. At offset +6 is 5AM, the next UTC day 
    UTC 22:00:00, At offset +2 is 12AM the next UTC day. At offset +7 is 5AM, the next UTC day
    UTC 21:00:00, At offset +3 is 12AM the next UTC day. At offset +8 is 5AM, the next UTC day
    UTC 20:00:00, At offset +4 is 12AM the next UTC day. At offset +9 is 5AM, the next UTC day
    UTC 19:00:00, At offset +5 is 12AM the next UTC day. At offset +10 is 5AM, the next UTC day
    UTC 18:00:00, At offset +6 is 12AM the next UTC day. At offset +11 is 5AM, the next UTC day
    UTC 17:00:00, At offset +7 is 12AM the next UTC day. At offset +12 is 5AM, the next UTC day
    UTC 16:00:00, At offset +8 is 12AM the next UTC day. At offset -11 is 5AM, this UTC day 
    UTC 15:00:00, At offset +9 is 12AM the next UTC day. At offset -10 is 5AM, this UTC day 
    UTC 14:00:00, At offset +10 is 12AM the next UTC day. At offset -9 is 5AM, this UTC day 
    UTC 13:00:00, At offset +11 is 12AM the next UTC day. At offset -8 is 5AM, this UTC day 
    UTC 12:00:00, At offset +12 is 12AM the next UTC day. At offset -7 is 5AM, this UTC day 
    UTC 11:00:00, At offset -11 is 12AM on the UTC day. At offset -6 is 5AM, this UTC day 
    UTC 10:00:00, At offset -10 is 12AM on the UTC day. At offset -5 is 5AM, this UTC day 
    UTC 09:00:00, At offset -9 is 12AM on the UTC day. At offset -4 is 5AM, this UTC day 
    UTC 08:00:00, At offset -8 is 12AM on the UTC day. At offset -3 is 5AM, this UTC day 
    UTC 07:00:00, At offset -7 is 12AM on the UTC day. At offset -2 is 5AM, this UTC day 
    UTC 06:00:00, At offset -6 is 12AM on the UTC day. At offset -1 is 5AM, this UTC day  
    UTC 05:00:00, At offset -5 is 12AM on the UTC day. At offset 0 is 5AM, this UTC day
    UTC 04:00:00, At offset -4 is 12AM on the UTC day. At offset +1 is 5AM, this UTC day
    UTC 03:00:00, At offset -3 is 12AM on the UTC day. At offset +2 is 5AM, this UTC day
    UTC 02:00:00, At offset -2 is 12AM on the UTC day. At offset +3 is 5AM, this UTC day
    UTC 01:00:00, At offset -1 is 12AM on the UTC day. At offset +4 is 5AM, this UTC day
    UTC 00:00:00, At offset 0 is 12AM on the UTC day. At offset +5 is 5AM, this UTC day

因此,例如,在UTC 8AM的returnThisHourOffset(5)应该返回-3与今天的UTC日期。虽然UTC时间下午9点(21:00:00)的returnThisHourOffset(5)应该在明天的UTC日期返回8(因为它是今天上午9点,而8点后是明天上午5点)。

显然,我在夏令时或抵消方面的表现并不多。 12.只是试图保持简单,它仍然看起来非常复杂。

这是我的代码,它严格用于获取午夜发生的偏移或可能是returnThisHourOffset(0)我正在寻找的任何时间(0-23)。

if(gmdate('A') == 'PM'){
        $offset = (24 - gmdate('G'));
        $today = gmdate("Y-m-d\T00\:00\:00", strtotime('tomorrow UTC'));
    } else {
        $offset = -1*(gmdate('G'));
        $today = gmdate("Y-m-d\T00\:00\:00", strtotime('today UTC'));
    }

我确信我的代码不是这个的答案,所以我希望其他人试图以某种方式解决这个问题。任何见解都表示赞赏。

1 个答案:

答案 0 :(得分:0)

这个看似它有效,但我可以接受更好的建议(因为它显然非常混乱)。

$utcHour = gmdate('G');
$timeofday = 5; // (0-23)

if($utcHour <= $timeofday){
    $offset = $timeofday - $utcHour;
    $today = gmdate("Y-m-d\T00\:00\:00", strtotime('today UTC'));
} elseif ($utcHour > $timeofday && $utcHour - $timeofday < 12){
    $offset = -1*($utcHour - $timeofday);
    $today = gmdate("Y-m-d\T00\:00\:00", strtotime('today UTC'));
} else {
    $offset = (24 - $utcHour) + $timeofday;
    $today = gmdate("Y-m-d\T00\:00\:00", strtotime('tomorrow UTC'));    
}