php methods for getting start and end timestamps for today yesterday last week and this month

  • 2020-09-16 07:24:09
  • OfStack

directory

1. Summary

The PHP mktime() function is used to return an Unix timestamp for 1 date.

1. How do mktime() get the start time and end time of yesterday?

2. php's method for getting start and end timestamps for today, yesterday, last week, and this month
1. mktime()
2. strtotime()

1. Summary

The mktime mktime() function is used to return an Unix timestamp for 1 date.

mktime()//php gets the start and end timestamps for today

$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 the start and end timestamps of yesterday


$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 and end timestamps 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 and end timestamps of the 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 an Unix timestamp for 1 date.

php method to get the start and end timestamps of today, yesterday, last week, and this month

1. mktime()

php's method of getting start and end timestamps for today, yesterday, last week, and this month USES php's time function mktime. Let's get straight to the point with an example of how to use mktime to get start and end timestamps for today, yesterday, last week, and this month, and then follow up with introduction 1 on the mktime function and how to use it.


//php Gets today's start and end timestamps 
$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 the start and end timestamps of yesterday 
$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 and end timestamps 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 and end timestamps of the 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 an Unix timestamp for 1 date.

grammar

mktime(hour,minute,second,month,day,year,is_dst)

参数 描述
hour 可选。规定小时。
minute 可选。规定分钟。
second 可选。规定秒。
month 可选。规定用数字表示的月。
day 可选。规定天。
year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。
is_dst

可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。

自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。

usage

The parameter always indicates the GMT date, so is_dst has no effect on the result.
The parameters can be left to right and are set to the current GMT value.
Note that before PHP 5.1, if the argument to this function was invalid, false would be returned.

It is also important to note that this function is useful for date arithmetic and validation. It automatically corrects out-of-bounds inputs such as:

[

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

]

Output results are as follows:

Jan-05-2020

2.


// For today, 00:00
$todaystart = strtotime(date('Y-m-d'.'00:00:00',time()));
// For today, 24:00
$todayend = strtotime(date('Y-m-d'.'00:00:00',time()+3600*24));
// Count the number of users registered today 
$todayuser['create_time'] = array(between,"$todaystart,$todayend");
$todaysum = $Users->where($todayuser)->count();

// For yesterday 00:00
$timestart = strtotime(date('Y-m-d'.'00:00:00',time()-3600*24));
// For today, 00:00
$timeend = strtotime(date('Y-m-d'.'00:00:00',time()));
// Count the number of users who signed up yesterday 
$map['create_time'] = array(between,"$timestart,$timeend");
$daycount = $Users->where($map)->count();

$this->assign("todaysum",$todaysum);
$this->assign("daycount",$daycount);

This article has been introduced to this, the need for friends can refer to 1.


Related articles: