php

How To Add Days With the Date in PHP

The future date or the particulate date can be calculated by adding the date with the current date or the specific date. PHP has many built-in functions for adding days with a particular date. The date_add() and strtotime() functions can be used for adding days with a date in PHP. The date_add() function is used with the date object created from the DateTime class or the date value created by using the date_create() function. The strtotime() function is used with any date value. The uses of these functions to add days with any date value have been shown in this tutorial.

Add Days With a Date by Using the date_add() Function

The value in a day, month, year, hour, minute, and second can be added with a date value to generate a new date using the date_add() function. The syntax of this function is given below:

Syntax:

date_add(date_object, interval_value)

 
This function can take two argument values, and both arguments are mandatory. The first argument is used to take the date object, and the second argument is used to take the interval value to generate a new date based on the interval value. Different uses of the date_add() function are shown in this part of the tutorial.

Example 1: Add Days by Using date_interval_create_from_date_string() Function

Create a PHP file with the following script that will assign the current date in a date object and add days in different ways using the date_add() function. The date_format() function prints the current date and the newly generated date in a particular format. The date_interval_create_from_date_string() function has been used in the script to add an interval with the current date. The first new date will be generated by using 5 days, and the second new date will be generated by using 5 months and 10 days.

<?php
 
//Assign a date value
$dateVal = date_create("Now");

echo "Today is ", date_format($dateVal, "d-M-Y"), "<br/>";

//Use date_add() function to add 5 days
date_add($dateVal, date_interval_create_from_date_string("5 days"));

//Display the new date
echo "The date after 5 days is ", date_format($dateVal, "d-M-Y"), "<br/>";

//Use date_add() function to add 2 months 10 days
date_add($dateVal, date_interval_create_from_date_string("2 months + 10 days"));  

//Display the new date
echo "The date after 2 months 15 days is ", date_format($dateVal, "d-M-Y");  

?>

 
The following output will appear after executing the previous script:

Example 2: Add Days by Using DateInterval Class

Create a PHP file with the following script that will assign a particular date in a date object and add days using the date_add() function and DateInterval class. The date_format() function prints the date in a particular format. The DateInterval function has been used in the script to add 6 months and 15 days with the particular date.

<?php
   //Assign a particular date
   $dateVal = date_create("01-Oct-2022");
   
   //Print assigned date value
   echo "The date is ", date_format( $dateVal, "D, d M Y"), "<BR/>";
   
   //Add interval with the date
   $newDate = date_add($dateVal, new DateInterval('P06M15D'));  
   
   //Print the new date
   echo "The date after 6 months 15 days ", date_format( $newDate, "D, d M Y");
 
?>

 
The following output will appear after executing the previous script:

Example 3: Add Days by Using DateTime and DateInterval Class

Create a PHP file with the following script that will assign a particular date in a date object by using the DateTime class and add days by using add() function and DateInterval class. The date_format() function prints the date in a particular format. The DateInterval function has been used in the script to add 2 years, 6 months, and 5 days with the particular date.

<?php

  //Assign a particular date
  $dateVal = new DateTime("25-Sep-2022");
 
  //Print assigned date value
  echo "The date is ", date_format( $dateVal, "D, d M Y"), "<BR/>";

  //Add the interval with the date
  $dateVal->add(new DateInterval('P2Y6M5D'));

  //Print the new date
   echo "The date after 2 years 6 months and 5 days is ", date_format( $dateVal, "D, d M Y");

?>

 
The following output will appear after executing the previous script:

Add Days With a Date by Using the strtotime() Function

Another way to add days with a date is to use the strtotime() function. This function generates the timestamp value of a particular date or after adding/subtracting days with the particular date. The syntax of this function is given below:

Syntax:

strtotime( $datetime, $time)

 
This function has two arguments. The first argument is mandatory and the second argument is optional. The date value of the format, “yyyy-mm-dd”, is taken in the first argument, and the string of time intervals is taken in the second argument of this function. The function returns the timestamp value of a date based on the argument values. Different uses of the strtotime() function have been shown in this part of the tutorial.

Example 4: Add Days by Adding Days in the Interval

Create a PHP file with the following script that will add 15 days with the particular date and print the newly generated date using the strtotime() function:

<?php

 
  //Assign a date as a string
  $dateVal = "2022-09-30";

  //Print assigned date value
  echo "The assigned date is ", $dateVal, "<BR/>";

  //Print the date after adding 15 days
  echo "The date after 15 days is ",date('d-M-Y', strtotime($dateVal. ' + 15 days'));

 
?>

 
The following output will appear after executing the previous script:

Example 5: Add Days by Adding Days and Months in the Interval

Create a PHP file with the following script that will add 10 days and 3 months with the particular date and print the newly generated date by using the strtotime() function:

<?php

 
  //Assign the day, month, and year of a date
  $day = "15";
  $month = "10";
  $year = "2022";

  //Print assigned date value
  echo "The assigned date is $day-$month-$year <BR/>";

  //Print the date after adding 15 days
  echo "The date after 10 days 3 Months is ",date('d-m-Y', strtotime($year.$month.$day. ' + 10 days 3 Months'));

 
?>

 
The following output will appear after executing the previous script:

Conclusion

The ways of adding days with the current date or the particular date by using date_add() and strtotime() functions have been shown in this tutorial using multiple examples. The new PHP users can properly add days with a date after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.