PHP: Calculate the difference in weeks between two dates.

This is a PHP tutorial on how to get the number of weeks that occur between two dates. In the past, I 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 have expand on that code and do a small bit of 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 have 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 depends on what your PHP script is supposed to achieve. However, in most cases, it makes sense to round the number down.

Why? Because if a full 7 days have not passed, then it is usually not considered to be a full week.

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. Yet the function still returned “30 weeks”.

Facebook Comments