php Implementation Gets Time Examples of Recent Days and Months

  • 2021-12-12 08:16:03
  • OfStack

In this paper, an example is given to describe the implementation of php to obtain the time of recent days and months. Share it for your reference, as follows:


<?php
date_default_timezone_set('Asia/Shanghai');
echo " Today :".date("Y-m-d H:i:s")."<br>";
echo " Yesterday :".date("Y-m-d",strtotime("-1 day")), "<br>";
echo " Tomorrow :".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "1 Week after :".date("Y-m-d",strtotime("+1 week")). "<br>";
echo "1 Week before :".date("Y-m-d",strtotime("-1 week")). "<br>";
echo "1 Week zero two days 4 Hours and two seconds later :".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";
echo " Next week 4:".date("Y-m-d",strtotime("next Thursday")). "<br>";
echo " Last week 1:".date("Y-m-d",strtotime("last Monday"))."<br>";
echo "1 Months ago :".date("Y-m-d",strtotime("last month"))."<br>";
echo "1 Months later :".date("Y-m-d",strtotime("+1 month"))."<br>";
echo "10 Years later :".date("Y-m-d",strtotime("+10 year"))."<br>";
echo '<hr/>';
//php Get 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;
echo " Today start timestamp and end timestamp ",' Start: ',$beginToday,' End: ',$endToday,'<br/>';
echo " Start time today :",date("Y-m-d H:i:s",$beginToday),'<br/>';
echo " End of the day :",date("Y-m-d H:i:s",$endToday),'<br/>';
echo '<hr/>';
//php Get 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;
echo " Yesterday start timestamp and end timestamp ",' Start: ',$beginYesterday,' End: ',$endYesterday,'<br/>';
echo " Yesterday's start time :",date("Y-m-d H:i:s",$beginYesterday),'<br/>';
echo " Yesterday's end time :",date("Y-m-d H:i:s",$endYesterday),'<br/>';
echo '<hr/>';
//php Get the last week's start and end timestamps 
$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'));
echo " Last week start timestamp and end timestamp ",' Start: ',$beginLastweek,' End: ',$endLastweek,'<br/>';
echo " Last week's start time :",date("Y-m-d H:i:s",$beginLastweek),'<br/>';
echo " End of last week :",date("Y-m-d H:i:s",$endLastweek),'<br/>';
echo '<hr/>';
//php Get the start and end timestamps of this month 
$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
$endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y'));
echo " Start and end timestamps of this month ",' Start: ',$beginThismonth,' End: ',$endThismonth,'<br/>';
echo " Start time of this month :",date("Y-m-d H:i:s",$beginThismonth),'<br/>';
echo " End of this month :",date("Y-m-d H:i:s",$endThismonth),'<br/>';
?>

Run results:

Today: 2019-07-06 10:23:11
Yesterday: July 5, 2019
Tomorrow: July 7, 2019
One week later: July 13, 2019
1 week ago: June 29, 2019
1 week, 2 days, 4 hours and 2 seconds later: 2019-07-15 14:14:13
Next week 4:20 19-07-11
Last week 1: 2019-07-01
1 month ago: June 6, 2019
One month later: August 06, 2019
Ten years later: 2029-07-06

--------------------------------------------------------------------------------
Today start timestamp and end timestamp start: 1562342400 end: 1562428799
Start today: 00:00:00 on July 06, 2019
End time today: 2019-07-06 23:59:59

--------------------------------------------------------------------------------
Yesterday start timestamp and end timestamp start: 1562256000 end: 1562342399
Yesterday's start time: 09-07-05 00:00: 00
Yesterday's end time: 2019-07-05 23:59:59

--------------------------------------------------------------------------------
Last week's start timestamp and end timestamp start: 1561305600 end: 1561910399
Last week's start time: 2019-06-2400: 00:00
Last week ended at 23:59:59 on June 30, 2019

--------------------------------------------------------------------------------
Month start timestamp and end timestamp start: 1561910400 end: 1564588799
Start time of this month: 00:00:00 on July 1, 2019
End of this month: 2019-07-31 23:59:59

PS: Here are some time and date related tools for your reference:

Online date/day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online date calculator/difference days calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online Date-Day Difference Calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Date and Time Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Introduction to php Object-Oriented Programming", "Introduction to php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: