The Wayback Machine - https://web.archive.org/web/20250624035109/https://www.geeksforgeeks.org/php/how-to-add-days-to-date-in-php/
Open In App

How to add days to $Date in PHP?

Last Updated : 24 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In PHP, adding days to date involves manipulating date values to compute future or past dates. This is typically done using the DateTime class, which provides robust methods for date and time arithmetic, allowing for precise and flexible date calculations.

Here we have some common methods to add days to $Date in PHP :

Method 1: Using strtotime() Function:

The strtotime() Function is used to convert an English textual date-time description to a UNIX timestamp. 
Syntax: 

strtotime( $EnglishDateTime, $time_now )

Parameters: This function accepts two optional parameters as mentioned above and described below. 

  • $EnglishDateTime: This parameter specifies the English textual date-time description, which represents the date or time to be returned.
  • $time_now: This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.


Example : PHP program to add days to $Date in PHP using strtotime() function. 
 

php
<?php
// PHP program to add days to $Date

// Declare a date
$Date = "2019-05-10";

// Add days to date and display it
echo date('Y-m-d', strtotime($Date. ' + 10 days'));

?>

Output
2019-05-20

Method 2: Using date_add() Function:

The date_add() function is used to add days, months, years, hours, minutes and seconds.

Syntax: 

date_add(object, interval);

Parameters: This function accepts two parameters as mentioned above and described below: 

  • Object: It specifies the DateTime object returned by date_create() function.
  • Interval: It specifies the DateInterval object.

Example : PHP program to add days to $Date in PHP using date_add() function. 
 

php
<?php
// PHP program to add 10 days in date 

// Declare a date
$date = date_create("2019-05-10");

// Use date_add() function to add date object
date_add($date, date_interval_create_from_date_string("10 days"));

// Display the added date
echo date_format($date, "Y-m-d");

?>

Output
2019-05-20

Method 3: Using date_modify() function

This PHP script initializes a DateTime object with the date '2024-06-23', adds 7 days using modify('+7 days'), formats the result as 'Y-m-d', and outputs '2024-06-30'. It demonstrates date manipulation using PHP's DateTime class.

Example:

PHP
<?php
// Original date
$originalDate = '2024-06-23';

// Create a DateTime object
$date = new DateTime($originalDate);

// Add 7 days to the date
$date->modify('+7 days');

// Format and output the modified date
echo $date->format('Y-m-d');  // Output: 2024-06-30
?>

Output
2024-06-30

Next Article

Similar Reads