Программно определить, используя время UTC, с каким смещением день / время

У меня есть задание 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**).
}

Я написал, что происходит в каждую полночь и 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

Так, например, returnThisHourOffset(5) в UTC 8AM должен вернуть -3 с сегодняшней датой UTC. В то время как returnThisHourOffset(5) в UTC 9 вечера (21:00:00) должен вернуть 8 с завтрашней датой в формате UTC (так как сегодня 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'));
    }

Я уверен, что мой код не является ответом на это, поэтому я надеюсь, что кто-то еще пытался каким-то образом решить эту проблему раньше. Любое понимание приветствуется.


person Donnie D'Amato    schedule 15.04.2015    source источник
comment
Примерно так, stackoverflow.com / questions / 7276304 / может быть?   -  person chris85    schedule 15.04.2015
comment
Я не думаю, что мне нужно использовать часовой пояс.   -  person Donnie D'Amato    schedule 15.04.2015


Ответы (1)


Это похоже работает, но я открыт для лучших предложений (поскольку это, очевидно, очень беспорядочно).

$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'));    
}
person Donnie D'Amato    schedule 15.04.2015