In this article we will see how to find the no of days between two dates using php.
The easiest way to calculate the no of days between two dates is by using the strtotime()
function to convert the given start and end date strings to Unix timestamps, and then subtract one timestamp from the other.
The result will be the number of seconds between the two dates, which can be divided by the number of seconds in a day (86400) to get the number of days.
Following is an example of how to calculate the no of days between two dates.
$date1 = '2023-03-26';
$date2 = '2023-04-02';
$days = floor((strtotime($date2) - strtotime($date1)) / 86400);
echo $days; // Output: 7
In the above example, we defined the $date1 & $date2 with the start and end date and these dates are in the format of “YYYY-MM-DD“.
We then use the strtotime()
function to convert each date string to a Unix timestamp and subtract the Unix timestamp of the first date from the Unix timestamp of the second date
Finally, divide the result by the number of seconds in a day (86400) to get the number of days between the two dates