Home > php教程 > php手册 > PHP求天数常犯的错误

PHP求天数常犯的错误

WBOY
Release: 2016-06-13 10:49:45
Original
998 people have browsed it

按N天分隔时间, 求出每一个时间段的开始和结束. 例如按7天分隔里头, 求当前时间所处的区间的开始和结束. 要注意时区!
注意, 不要简单地对时间戳取模, 而是要对时间戳与开始时间(1970-01-01)的距离进行取模.

$step = 7;
$zerotime = strtotime('1970-01-01'); // 不一定是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);
?>
注意: Asia/Chongqing 时区, php 5.2.14
strtotime('1980-5-1 01:00:00') - strtotime('1980-5-1 00:00:00'); // 输出 0!

 

 

作者 ideawu

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template