Get the number of days between two dates in PHP
Question:
How to get the number of days between two dates in PHP? Answer:
$startDate = new DateTime("2019-10-27");
$endDate = new DateTime("2020-04-11");
$difference = $endDate->diff($startDate);
echo $difference->format("%a");
Description:
To get the difference between two dates first you need to create a start date and an end date DateTime
objects. Afterward, simply use the diff
method on the end date and use the start date as a parameter. The end result is a DateInterval object that can be formatted to as you want.
Reference:
The DateInterval format reference
Share "How to get the number of days between two dates in PHP?"
Related snippets:
- Add minutes to date time in PHP
- Add days to date in PHP
- Get same day in the next week in PHP
- Get time difference in minutes in PHP
- Convert date to timestamp in PHP
- Convert timestamp to DateTime in PHP
- Get the last day of a month from date in PHP
- Get the number of days between two dates in PHP
- Get yesterday's date in PHP
- Get tomorrow's date in PHP
- Get current time in PHP
- Get actual date in PHP
Tags:
number of days between two dates, difference between two dates, difference, dates, php Technical term:
Get the number of days between two dates in PHP