Summary of Examples of PHP Common Date Addition and Subtraction Calculation Methods

  • 2021-10-27 06:38:33
  • OfStack

This paper summarizes the common date addition and subtraction calculation methods of PHP with examples. Share it for your reference, as follows:

PHP Standard Date Format


date("Y-m-d H:i:s");

PHP Simple Date Addition and Subtraction Calculation


<?php
  date_default_timezone_set('PRC'); // Default time zone 
  echo " Today :",date("Y-m-d",time()),"\n";
  echo " Today :",date("Y-m-d",strtotime("18 june 2008")),"\n";
  echo " Yesterday :",date("Y-m-d",strtotime("-1 day")),"\n";
  echo " Tomorrow :",date("Y-m-d",strtotime("+1 day")),"\n";
  echo "1 Week after :",date("Y-m-d",strtotime("+1 week")),"\n";
  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")), "\n";
  echo " Next week 4:",date("Y-m-d",strtotime("next Thursday")),"\n";
  echo " Last week 1:".date("Y-m-d",strtotime("last Monday"))."\n";
  echo "1 Months ago :".date("Y-m-d",strtotime("last month"))."\n";
  echo "1 Months later :".date("Y-m-d",strtotime("+1 month"))."\n";
  echo "10 Years later :".date("Y-m-d",strtotime("+10 year"))."\n";
?>

Run results:

Today: July 31, 2018
Today: 2008-06-18
Yesterday: July 30, 2018
Tomorrow: August 1st, 2018
One week later: August 7, 2018
1 week, 2 days, 4 hours and 2 seconds later: 2018-08-09 15:15:40
Next week at 4:18-08-02
Last week at 1: 07-30, 2018
1 month ago: July 1, 2018
One month later: August 31, 2018
Ten years later: July 31, 2028

PHP Advanced Date Addition and Subtraction Calculation


<?php
  //  Default time zone 
  date_default_timezone_set('PRC');
  echo " Today : ", date('Y-m-d H:i:s'), "\n";    //  Output the current time 
  echo " Tomorrow : ", date('Y-m-d H:i:s', strtotime('+1 day')), "\n";    //  Output tomorrow time 
  //strtotime  Acceptable 2 Parameters, type timestamp For the specified date 
  echo date('Y-m-d H:i:s', strtotime ("+1 day", strtotime('2017-11-11'))), "\n";
  //  Here +1 day  You can modify parameters 1 For any number you want 
  // day It can also be changed to year (Year) month (Months) hour (Hours) minute (Points) second (Seconds) 
  echo date('Y-m-d H:i:s', strtotime("+1 day +1 hour +1 minute")), "\n";
?>

Run results:

Today: 2018-07-31 11:37:11
Tomorrow: 2018-08-01 11:37:11
2017-11-12 00:00:00
2018-08-01 12:38:11

PHP Advanced Calculation Date Time Difference


<?php
  //  Default time zone 
  date_default_timezone_set('PRC');
  echo date("Y-m-d H:i:s"), "\n";
  //  Date and day addition function 
  echo date('Y-m-d', strtotime('+1 day', strtotime('2016-09-12'))), "\n\n";
  echo strtotime('2016-09-12'), "\n";
  echo date("Y-m-d", '1473609600'), "\n";
  echo date("Y-m-d", '1573609600'), "\n";
  //  Date and day addition function 
  $d = "2016-09-12 10:12:20";
  echo date("Y-m-d", strtotime("$d  +1  day")), "\n\n";
  //  Convert a date into a time cut 
  function date2time($d){
    $year=((int)substr("$d",0,4)); // Acquisition year 
    $month=((int)substr("$d",5,2)); // Acquisition month 
    $day=((int)substr("$d",8,2));  // What number do you get 
    return mktime(0,0,0,$month,$day,$year);
  }
  echo date2time("2016-09-12"), "\n\n";
  $date_1="2066-09-12";
  $date_2="2016-09-12";
  $Date_List_a1=explode("-",$date_1);
  $Date_List_a2=explode("-",$date_2);
  $d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);
  $d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);
  $Days=round(($d1-$d2)/3600/24);
  echo "$date_2 -> $date_1  Difference : $Days  Days ", "\n";
?>

Run results:

2018-07-31 11:37:39
2016-09-13

1473609600
2016-09-12
2019-11-13
2016-09-13

1473609600

2016-09-12 - > 2066-09-12 difference:-17056 days

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 Syntax", "Summary of PHP Operation and Operator Usage", "Introduction to php Object-Oriented Programming" and "Summary of php String (string) Usage"

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


Related articles: