Home  >  Article  >  php教程  >  PHP implements data statistics functions for today, yesterday, last week, this week, and this month

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

大家讲道理
大家讲道理Original
2016-11-11 09:18:255570browse

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'));


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