It is very simple to get the starting timestamp and ending timestamp of today, yesterday, last week, and this month in php

PHP's method of obtaining the start timestamp and end timestamp of today, yesterday, last week, and this month mainly uses PHP's time function mktime. The following first uses an example to illustrate how to use mktime to obtain the start timestamp and end timestamp of today, yesterday, last week, and this month, and then introduces the function and usage of the mktime function. Very simple.

php gets today's start timestamp and end timestamp

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

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

grammar

mktime(hour,minute,second,month,day,year,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 argument 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, it will return false.

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

echo(date("M-d-Y",mktime(0,0,0,12,36,2001)));

The output will be as follows:

Jan-05-2002

Guess you like

Origin blog.csdn.net/hanzhuhuaa/article/details/132690706