Home > Article > Backend Development > How to get the start timestamp and end timestamp of today, yesterday, last week, and this month in php_PHP Tutorial
//php gets yesterday’s start timestamp and end timestamp
$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 gets the start timestamp and end timestamp of last week
$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 Get the start timestamp and end timestamp of this month
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
PHP mktime() function is used to return the Unix timestamp of a date.
Syntax
mktime(hour,minute,second,month,day,year,is_dst)
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.
As of 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.
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 parameters of this function are 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: