У меня есть работа 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
Так, например returnThisHourOffset(5)
в UTC 8 утра должен вернуть -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'));
}
Я уверен, что мой код не является ответом на этот вопрос, поэтому я надеюсь, что кто-то еще пытался справиться с этим раньше каким-то образом. Любое понимание приветствуется.
это похоже это работает, но я открыт для лучших предложений (так как это, очевидно, очень грязно).
$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'));
}
Других решений пока нет …