Home > php教程 > PHP源码 > body text

PHP implements data statistics functions for today, yesterday, last week, this week, and this month

大家讲道理
Release: 2016-11-11 09:18:25
Original
5682 people have browsed it

Application scenario

Count the number of articles published by a person according to today, yesterday, last week, this week, and this month

Principle analysis

Suppose there is a field in the article table to store the timestamp of article creation (cdate), For example, today (2016-11-8), then the query condition is cdate >= 2016-11-8 00:00 AND cdate

Implementation plan

According to the above analysis, it is necessary If you know today's start timestamp and end timestamp, then yesterday, last week, and this week are similar. Use PHP's mktime function to obtain the start and end timestamps.

mktime()

Syntax: mktime(hour,minute,second,month,day,year)

PHP implements data statistics functions for today, yesterday, last week, this week, and this month

Code implementation

//php获取今日开始时间戳和结束时间戳
$today_start=mktime(0,0,0,date('m'),date('d'),date('Y'));
$today_end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//php获取昨日起始时间戳和结束时间戳
$yesterday_start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$yesterday_end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
//php获取上周起始时间戳和结束时间戳
$lastweek_start=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$lastweek_end=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
//php获取本周周起始时间戳和结束时间戳
$thisweek_start=mktime(0,0,0,date('m'),date('d')-date('w'+1),date('Y'));
$thisweek_end=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
//php获取本月起始时间戳和结束时间戳
$thismonth_start=mktime(0,0,0,date('m'),1,date('Y'));
$thismonth_end=mktime(23,59,59,date('m'),date('t'),date('Y'));
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!