search
Homephp教程PHP源码PHP implements data statistics functions for today, yesterday, last week, this week, and this month

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools