- PHP

Find how many weeks are between two dates in PHP

Use the combination of DateTime(), floor(), diff(), and format() functions.

  • Count the difference between two dates in days.
  • Divide the total days number by 7 to find the number of weeks.

Example:

Find how many weeks are between two given dates in PHP.

<?php
    $firstDate = new DateTime("2021-07-06");
    $secondDate = new DateTime("2021-07-30");

    $weeksBetween = floor($secondDate->diff($firstDate)->format("%a") / 7);

    echo $weeksBetween;
?>

Output: 3

Weeks between two dates PHP function

Here is a ready-to-use function that will help you find how many weeks are between two given dates.

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

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

    // Example:
    echo weeksBetweenDates("01.04.2019", "25.05.2019"); // Output: 7
?>