In this tutorial, you shall learn how to find the number of days between two given dates in PHP using date_diff() function, with example programs.

PHP – Find number of days between two given dates

To find the number of days between two dates in PHP, call date_diff() date/time function, pass the two dates as argument to it, and access the $days property of the object returned by the date_diff() function.

Examples

ADVERTISEMENT

1. Find number of days between two given dates: date1 – date2

In the following program, we shall take two dates in $date1 and $date2 and find the number of days between these two dates.

PHP Program

<?php
$date1 = date_create( "2022-01-15 12:23:17" );
$date2 = date_create( "2022-02-22 11:05:25" );
$diff = date_diff( $date1, $date2 );
$result = $diff -> days;
echo "Number of days : ", $result;
?>

Output

PHP - Find Number of Days between Two Dates

Conclusion

In this PHP Tutorial, we learned how to find the number of days between two dates in PHP, using date_diff() function.