PHP gets the example code of this year, this month, this week timestamp and date format

Time:2021-9-16

Timestamp format:

//Get 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; 
 //Get 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; 
 //Get the start timestamp and end timestamp of the week  
 $beginThisweek = mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y')); 
 $endThisweek=time(); 
 //Get the start timestamp and end timestamp of the 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')); 
 //Get the start timestamp and end timestamp of the current month 
 $beginThismonth=mktime(0,0,0,date('m'),1,date('Y')); 
 $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y')); 
 //Start time of last month: 
 $begin_time = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month'))); 
 $end_time = strtotime(date("Y-m-d 23:59:59", strtotime(-date('d').'day'))); 
 $begin_ year = strtotime(date("Y",time())."-1"."-1"); // Beginning of the year 
 $end_ year = strtotime(date("Y",time())."-12"."-31"); // End of the year 
 //The timestamp of the difference between the current time and the early morning of the next day 
 $time = (strtotime(date('Y-m-d'))+3600*24) - time() ;

Date format:

//Start time of last week
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y")));
//Start time of the week
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
//Start time of last month
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y")));
//Starting time of this month
echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
//Starting time of this year
echo date(‘Y-01-01'); 
echo date(‘Y-12-31');

Gets the last and first days of the month


 $BeginDate=date('Y-m-01', strtotime(date("Y-m-d")));
  echo $BeginDate;
  echo "
";
  echo date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
  echo "
";

Get the year, month, and days of the current month

Echo "total of this month:". Date ("t"). "Days";
  Echo "current year". Date ('y ');
  Echo "current month". Date ('m ');
  Echo "current date". Date ('d ');
  echo "
";

Gets the number of days or the last day of the specified month

$days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);
  Echo "return days of April 2011".. $days“
";
  $days = date('t', strtotime("2011-4-1"));
  Echo "return days of April 2011".. $days“
";
  $days = date("t"); 
  Echo "days of the current month". Days“
";
 cal_ days_ in_ Common of month: returns the number of days of a month in a year in a calendar

Parameter introduction:

cal_days_in_month($par1,$par2,$par3);
$PAR1: some kind of calendar used for calculation, PHP calendar constant
$PAR2: select a month in the calendar for parameter $PAR1
$Par3: select a year in the calendar

Return value:

$PAR1 select the number of days of a year and a month in the calendar
PHP calendar constant: the calendar extension contains functions that simplify the conversion between different calendar formats.
  In order for these functions to work, you must compile PHP through — enable calendar. Windows has integrated support for calendar extension
Constant cal in case_ Gregorian is a pre-defined constant in PHP, just like PHP_ OS constant
Gregorian means Gregorian calendar; solar calendar

summary

The above is the example code of PHP to obtain the timestamp and date format of this year, this month and this week introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to the developeppaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!