我有一个每小时触发一次的 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 发生的事情。
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)
应返回 8 和明天的 UTC 日期(因为今天是上午 9 点,8 小时后是明天凌晨 5 点)。
显然,我不太喜欢夏令时或偏移量> 12。 只是试图保持简单,它看起来仍然非常复杂。
这是我的代码,严格用于获取午夜发生的偏移量或我正在寻找一天中的任何时间 (0-23) 可能returnThisHourOffset(0)
的偏移量。
if(gmdate('A') == 'PM'){
$offset = (24 - gmdate('G'));
$today = gmdate("Y-m-dT00:00:00", strtotime('tomorrow UTC'));
} else {
$offset = -1*(gmdate('G'));
$today = gmdate("Y-m-dT00:00:00", strtotime('today UTC'));
}
我相信我的代码不是这个问题的答案,所以我希望其他人以前曾尝试以某种方式解决这个问题。 任何见解都值得赞赏。
这看起来有效,但我愿意接受更好的建议(因为它显然非常混乱)。
$utcHour = gmdate('G');
$timeofday = 5; // (0-23)
if($utcHour <= $timeofday){
$offset = $timeofday - $utcHour;
$today = gmdate("Y-m-dT00:00:00", strtotime('today UTC'));
} elseif ($utcHour > $timeofday && $utcHour - $timeofday < 12){
$offset = -1*($utcHour - $timeofday);
$today = gmdate("Y-m-dT00:00:00", strtotime('today UTC'));
} else {
$offset = (24 - $utcHour) + $timeofday;
$today = gmdate("Y-m-dT00:00:00", strtotime('tomorrow UTC'));
}