Skip to content
PHP Logo

How can PHP be used to manipulate dates, such as adding or subtracting days?

To manipulate dates in PHP, you can use the DateTime class which provides methods for adding or subtracting days. You can create a new DateTime object with the desired date, then use the modify() method to add or subtract days. Finally, you can format the modified date using the format() method to get the result in the desired format.

php
$date = new DateTime('2022-01-01');
$date->modify('+3 days');
echo $date->format('Y-m-d'); // Output: 2022-01-04