How to add days to $Date in PHP?
Adding days to $Date can be done in many ways. It is very easy to do by using built-in functions like strtotime(), date_add() 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.
Program: PHP program to add days to $Date in PHP using strtotime() function.
<?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')); ?> |
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 mentined above and described below:
- Object: It specifies the DateTime object returned by date_create() function.
- Interval: It specifies the DateInterval object.
Program: PHP program to add days to $Date in PHP using date_add() function.
<?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"); ?> |
2019-05-20
Recommended Posts:
- Subtract days from Date object in JavaScript
- JavaScript | Adding days in milliseconds to Date object
- How to get the number of days in a specified month using JavaScript ?
- PHP | Number of week days between two dates
- How to calculate the number of days between two dates in javascript?
- Program to find the number of days between two dates in PHP
- PHP | Date and Time
- JavaScript | Set Date Methods
- jQuery UI | Date Picker
- Ruby Date and Time
- JavaScript | Date Objects
- Increment a given date in JavaScript
- AngularJS | date Filter
- How to check whether an object is a date?
- How to get the current date in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



