How to get the start timestamp and end timestamp of today, yesterday, last week, and this month in php

怪我咯
Release: 2023-03-13 10:30:01
Original
1514 people have browsed it

php method to get the starting timestamp and end timestamp of today, yesterday, last week, and this month mainly uses the time of phpfunction mktime. Let's go straight to the topic first and use mktime to illustrate how to use mktime to get the start timestamp and end timestamp of today, yesterday, last week, and this month. Then we will introduce the function and usage of mktime function

The code is as follows:

//php获取今日开始时间戳和结束时间戳
$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
//php获取昨日起始时间戳和结束时间戳
$beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

//php获取上周起始时间戳和结束时间戳

$beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

//php获取本月起始时间戳和结束时间戳

$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
Copy after login

PHP mktime() function is used to return the Unix timestamp of a date.

Syntax

mktime(hour,minute,second,month,day,year,is_dst)
Copy after login

Parameter Description
hour Optional. Specified hours.
minute Optional. Specified minutes.
second optional. Specifies seconds.
month Optional. Specifies the numeric month.
day Optional. Specify days.
year Optional. Specified year. On some systems, legal values ​​are between 1901 - 2038. However, this limitation no longer exists in PHP 5.
is_dst
Optional. Set to 1 if the time is during Daylight Saving Time (DST), 0 otherwise, or -1 if unknown.

Since 5.1.0, the is_dst parameter is deprecated. Therefore the new time zone handling features should be used.

Usage

The parameter always represents a GMT date, so is_dst has no effect on the result.

The parameters can be left empty in order from right to left, and the empty parameters will be set to the corresponding current GMT value.

Note that before PHP 5.1, if the parameter of this function is illegal, false will be returned.

Another thing to note is that this function is very useful for date operations and verification. It can automatically correct out-of-bounds input, such as:
The code is as follows:

echo(date("M-d-Y",mktime(0,0,0,12,36,2013)));
Copy after login

will output the result as: Jan-05-2014

The above is the detailed content of How to get the start timestamp and end timestamp of today, yesterday, last week, and this month in php. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template