PHP求天数常犯的错误_PHP教程

WBOY
Release: 2016-07-13 17:51:35
Original
848 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

www.bkjia.com true http://www.bkjia.com/PHPjc/478185.html TechArticle 按N天分隔时间, 求出每一个时间段的开始和结束. 例如按7天分隔里头, 求当前时间所处的区间的开始和结束. 要注意时区! 注意, 不要简单地...
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!