PHP: Calculate the difference in weeks between two dates.

In this PHP tutorial, we are going to show you how to get the number of weeks that occurred between two dates.

Not so long ago, we wrote a guide on how to calculate the difference between two dates.

However, that article only focused on years, months and days. In order to calculate the difference in weeks, we will need to expand on that code and do some extra math.

Take a look at the custom PHP function below:

/**
 * A custom function that calculates how many weeks occur
 * between two given dates.
 * 
 * @param string $dateOne Y-m-d format.
 * @param string $dateTwo Y-m-d format.
 * @return int
 */
function numWeeks($dateOne, $dateTwo){
    //Create a DateTime object for the first date.
    $firstDate = new DateTime($dateOne);
    //Create a DateTime object for the second date.
    $secondDate = new DateTime($dateTwo);
    //Get the difference between the two dates in days.
    $differenceInDays = $firstDate->diff($secondDate)->days;
    //Divide the days by 7
    $differenceInWeeks = $differenceInDays / 7;
    //Round down with floor and return the difference in weeks.
    return floor($differenceInWeeks);
}

The PHP function above takes in two date strings as parameters. It then creates two DateTime objects to represent these dates. As you can see, we’ve called these DateTime objects $firstDate and $secondDate.

Using the DateTime::diff function, we retrieved the difference in days. This is because the Datetime::diff function does not recognize “weeks” as a date format. It can return years, months or days, but not weeks.

As a result, we need to do our own math and divide the number of days by 7.

Round the week difference up or round it down?

At this stage, we need to make a decision. Do we want to round our weeks down or do we want to round them up? Obviously, this will depend on what your PHP script is supposed to achieve.

However, in most cases, it makes more sense to round the number down.

This is because it isn’t considered a full week until seven full days have passed.

For example, if there are 125 days between two dates, then that is 17.85 weeks.

By default, our floor function will round that decimal figure down to 17. This is because only 17 weeks have passed between the two dates. It has not reached the 18th week yet.

However, if you do want to round the week difference up, then you can simply replace floor in the code above with the ceil function:

//Using ceil instead of floor
return ceil($differenceInWeeks);

If for whatever reason you want to keep the decimal places, then you can remove the floor function altogether and just return the $differenceInWeeks as it is.

Examples of our custom function being used.

In the code below, you will find a few examples of the numWeeks function being used:

$example = numWeeks('2020-01-21', '2020-01-28');
var_dump($example); //1 week


$example = numWeeks(date("Y-m-d"), '2020-12-31');
var_dump($example); //30 weeks on May 31st 2020


$example = numWeeks('1998-01-21', '2020-05-31');
var_dump($example); //1166 weeks


$example = numWeeks('2020-05-31', '2020-05-31');
var_dump($example); //0 weeks


$example = numWeeks('2020-12-31', '2020-05-31');
var_dump($example); //30 weeks

As you can see, it doesn’t really matter which order the dates are in. If you look at the last example, you can see that the first date actually occurs at a later stage than the second date.

However, the function still returned “30 weeks”.