- PHP

Find how many days are between two dates in PHP

Use DateTime(), diff(), and format() functions.

Example:

Find how many days are between two given days in PHP.

<?php
    $firstDate = new DateTime("2022-03-05");
    $secondDate = new DateTime("2022-04-22");

    $daysBetween = $secondDate->diff($firstDate)->format("%a");

    echo $daysBetween;
?>

Output: 48

Days between dates PHP function

Alternatively, if you need to check these things often, you can use a function like this one:

<?php
function daysBetween($date1, $date2) {
    $firstDate = new DateTime($date1);
    $secondDate = new DateTime($date2);

    return $secondDate->diff($firstDate)->format("%a");
}

// Example:

echo daysBetween("2022-01-01", "2022-01-15"); // Output: 14
?>