Home  >  Article  >  Backend Development  >  Common mistakes in finding the number of days in PHP_PHP Tutorial

Common mistakes in finding the number of days in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:35891browse

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478185.htmlTechArticleSeparate time by N days and find the beginning and end of each time period. For example, divide it by 7 days, Find the start and end of the interval where the current time is located. Pay attention to the time zone! Note, don’t simply...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn