Home > php教程 > PHP源码 > PHP求天数常犯的错误详解

PHP求天数常犯的错误详解

WBOY
Release: 2016-06-08 17:23:37
Original
1012 people have browsed it

本文章来给大家介绍在利用php统计天数或两天之间的数据时一些小问题,下面我来给大家详细举例说明。

<script>ec(2);</script>

按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!
Related labels:
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