How can you calculate the difference between two dates in PHP?
To calculate the difference between two dates in PHP, you can use the DateTime class to create DateTime objects for the two dates and then calculate the difference using the diff() method. This method returns a DateInterval object which can be used to extract the difference in days, months, years, etc.
$date1 = new DateTime('2022-01-01');
$date2 = new DateTime('2022-02-01');
$interval = $date1->diff($date2);
echo "Difference: " . $interval->format('%R%a days');
Keywords
Related Questions
- How can using [php]-tags for example code help improve code readability and debugging in PHP?
- In what scenarios would it be more appropriate to use the explode() function in PHP instead of strstr() or substr()?
- What are the drawbacks of using the Auto Increment value to determine the number of records in a table in PHP?