How can PHP be used to calculate the difference between two dates?
To calculate the difference between two dates in PHP, you can use the DateTime class to create DateTime objects for each date, and then use the diff() method to calculate the difference between them. This method returns a DateInterval object which can provide the difference in terms of days, months, years, etc.
$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2022-01-10');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days');
Keywords
Related Questions
- What is the best approach for recursively searching a folder structure in PHP?
- What potential pitfalls should be considered when resizing and creating thumbnails for images in PHP?
- What potential issue could arise from not defining the variable $user in the PHP script when it comes from a form input?