Home >Backend Development >PHP Tutorial >How Can I Correctly Add Days to a MySQL Date Using PHP's `strtotime()` Function?
Adding Days to a MySQL Date in PHP
When working with dates in PHP, it is often necessary to add or subtract days to a given date. For instance, you may wish to calculate the date one week from today or the date two months ago.
One common method for adding days to a date is to use the strtotime() function. However, a common pitfall when using this method is to use the day suffix instead of the days suffix.
For example, the following code will actually subtract one day from the given date:
$date = '2010-09-17'; echo date('Y-m-d', strtotime($date . ' + 1 day')); // Outputs 2010-09-16
To correctly add one day to the date, you need to use the days suffix:
$date = '2010-09-17'; echo date('Y-m-d', strtotime($date . ' + 1 days')); // Outputs 2010-09-18
Additionally, you can use this method to add multiple days to a date:
$date = '2010-09-17'; echo date('Y-m-d', strtotime($date . ' + 2 days')); // Outputs 2010-09-19
The above is the detailed content of How Can I Correctly Add Days to a MySQL Date Using PHP's `strtotime()` Function?. For more information, please follow other related articles on the PHP Chinese website!