How to add days to $Date in PHP?
Last Updated :
24 Jun, 2024
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'));
?>
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");
?>
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
?>
Similar Reads
How to Subtract Days from Date in PHP? In this article, we will see how to subtract days from the given date object in PHP. There are two methods to subtract the date from the given date, these are: Table of Content Using PHP date_sub() functionUsing subtraction and PHP strtotime() functionUsing PHP date_sub() functionThe date_sub() func
2 min read
How to Calculate Business Days in PHP? We have given two dates, our task is to write a PHP program to get total business days, i.e. weekdays between the two dates. Below are the approaches to calculate business days in PHP: Table of Content Using DateTime and DateIntervalUsing strtotime and date functionsUsing DateTime and DateIntervalIn
2 min read
How to get the current Date and Time in PHP ? To get the current date and time in PHP, use the date() function, which formats UNIX timestamps into a human-readable format. It converts timestamps, measured in seconds since January 1, 1970, into a more understandable representation for users.Syntax: date('d-m-y h:i:s');Parameters: The date() has
2 min read
How to Extract Day, Month and Year in PHP ? Given a Date, the task is to extract day, month, and year from the date using PHP. There are various methods to extract day, month, and year, these are: Table of Content Using date() FunctionUsing DateTime ClassUsing getdate() FunctionApproach 1: Using date() FunctionThe date() function in PHP allow
2 min read
How to get last day of a month from date in PHP ? Given a date and the task is to print the last day of the month. We will use date() and strtotime() function to get the last day of the month. Used PHP functions:date() function: Format a local time/date.strtotime() function: Parse about any English textual datetime description into a Unix timestamp
2 min read