Home > Article > Backend Development > Common mistakes in finding the number of days in PHP_PHP Tutorial
Separate time by N days and find the start and end of each time period. For example, separate it by 7 days and find the start and end of the interval where the current time is. Pay attention to the time zone!
Note, do not simply modulo the timestamp, but modulo the distance between the timestamp and the start time (1970-01-01).
$step = 7;
$zerotime = strtotime('1970-01-01'); // Not necessarily 0!
$span = ($time - $zerotime) % ($step * 86400);
$stime = $time - $span;
$etime = $stime + ($step * 86400) - 1;
$sdate = date("Y-m-d H:i:s", $stime);
$edate = date("Y-m-d H:i:s", $etime);
?>
Note: Asia/Chongqing time zone, php 5.2.14
strtotime('1980-5-1 01:00:00') - strtotime('1980-5-1 00:00:00'); // Output 0!
Author ideawu