2015-04-15 3 views
1

У меня есть работа cron, которая срабатывает ежечасно. В моей базе данных есть строки, содержащие смещения UTC. Я пытаюсь сделать следующее программно.Программно определить время 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 

Так, например 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')); 
    } 

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

+0

Что-то вроде этого, http://stackoverflow.com/questions/7276304/php-setting-a-timezone-by-utc-offset возможно? – chris85

+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'));  
}