可能な実装:
// Europe/Bucharest is GMT+2 (GMT+3 during DST)
$tz = new DateTimeZone('Europe/Bucharest');
$now = new DateTime('now', $tz); // now (2016-05-01 14:50:23, local time)
$dayStart = clone $now;
$dayStart->setTime(0, 0, 0); // start of today (2016-05-01 00:00:00, local time)
$dayEnd = clone $now;
$dayEnd->setTime(23, 59, 59); // end of today (2016-05-01 23:59:59, local time)
// If you need the timestamps (to compare with MySQL TIMESTAMP columns)
printf("%d .. %d\n", $dayStart->getTimestamp(), $dayEnd->getTimestamp());
// It prints: 1462050000 .. 1462136399
// If you need the dates in UTC timezone (to compare with MySQL DATETIME columns)
$tzUTC = new DateTimeZone('UTC');
$dayStart->setTimezone($tzUTC);
$dayEnd->setTimezone($tzUTC);
printf("%s .. %s\n", $dayStart->format('Y-m-d H:i:s'), $dayEnd->format('Y-m-d H:i:s'));
// It prints: 2016-04-30 21:00:00 .. 2016-05-01 20:59:59
更新
現地時間ごとに3時間を得るためにDatePeriod
クラスを使用できます。
// A 3 hours interval
$interval = new DateInterval('PT3H');
// A period that starts at today's 00:00:00 (local time) and ends at 23:59:59
$period = new DatePeriod($dayStart, $interval, $dayEnd);
// Iterate over it to get the moments every 3 hours
foreach ($period as $moment) {
echo($moment->format('Y-m-d H:i:s')."\n");
}
上記のコードにこのコードを追加すると、UTCで表される日付が取得されます。あなたは$dayStart
と$dayEnd
のタイムゾーンを変更するコードのブロックをスキップする場合は、上記のコードは印刷されます:
$tzoffset = date('Z');
:
2016-05-01 00:00:00
2016-05-01 03:00:00
2016-05-01 06:00:00
2016-05-01 09:00:00
2016-05-01 12:00:00
2016-05-01 15:00:00
2016-05-01 18:00:00
2016-05-01 21:00:00
**このような計算はタイムスタンプでは行いません。 ['DateTime'](http://php.net/manual/ja/class.datetime.php)クラスとその親戚を使用してください。 – axiac
タイムスタンプはUTCだがローカルサーバー時間ではないと思うのはなぜですか? – splash58
@axiac次に、開始時間を3時間に揃える方法を提案しますか?モジュロは簡単です。 DateTimeクラスでこれを行うことはできますか? – ygoe