How can i calculate the number of weeks between two dates in php?

echo datediff('ww', '9 July 2003', '4 March 2004', false);

Find the function on the site below: http://www.addedbytes.com/code/php-datediff-function/

UPDATE

Link is now broken (Sept 2017), so function below pulled from webarchive:

<?php

/**
 * @param $interval
 * @param $datefrom
 * @param $dateto
 * @param bool $using_timestamps
 * @return false|float|int|string
 */
function datediff($interval, $datefrom, $dateto, $using_timestamps = false)
{
    /*
    $interval can be:
    yyyy - Number of full years
    q    - Number of full quarters
    m    - Number of full months
    y    - Difference between day numbers
           (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
    d    - Number of full days
    w    - Number of full weekdays
    ww   - Number of full weeks
    h    - Number of full hours
    n    - Number of full minutes
    s    - Number of full seconds (default)
    */

    if (!$using_timestamps) {
        $datefrom = strtotime($datefrom, 0);
        $dateto   = strtotime($dateto, 0);
    }

    $difference        = $dateto - $datefrom; // Difference in seconds
    $months_difference = 0;

    switch ($interval) {
        case 'yyyy': // Number of full years
            $years_difference = floor($difference / 31536000);
            if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
                $years_difference--;
            }

            if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
                $years_difference++;
            }

            $datediff = $years_difference;
        break;

        case "q": // Number of full quarters
            $quarters_difference = floor($difference / 8035200);

            while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
                $months_difference++;
            }

            $quarters_difference--;
            $datediff = $quarters_difference;
        break;

        case "m": // Number of full months
            $months_difference = floor($difference / 2678400);

            while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
                $months_difference++;
            }

            $months_difference--;

            $datediff = $months_difference;
        break;

        case 'y': // Difference between day numbers
            $datediff = date("z", $dateto) - date("z", $datefrom);
        break;

        case "d": // Number of full days
            $datediff = floor($difference / 86400);
        break;

        case "w": // Number of full weekdays
            $days_difference  = floor($difference / 86400);
            $weeks_difference = floor($days_difference / 7); // Complete weeks
            $first_day        = date("w", $datefrom);
            $days_remainder   = floor($days_difference % 7);
            $odd_days         = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?

            if ($odd_days > 7) { // Sunday
                $days_remainder--;
            }

            if ($odd_days > 6) { // Saturday
                $days_remainder--;
            }

            $datediff = ($weeks_difference * 5) + $days_remainder;
        break;

        case "ww": // Number of full weeks
            $datediff = floor($difference / 604800);
        break;

        case "h": // Number of full hours
            $datediff = floor($difference / 3600);
        break;

        case "n": // Number of full minutes
            $datediff = floor($difference / 60);
        break;

        default: // Number of full seconds (default)
            $datediff = $difference;
        break;
    }

    return $datediff;
}

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”.

How do you calculate the difference in weeks between two dates?

To calculate the number of weeks between two dates, start by counting the number of days between the start and end date. Then, divide that number by 7 days per week.

How do you calculate how many weeks?

Here are the steps needed to calculate the number of weeks between two dates. Divide the number of days by 7 and round down. You will get the number of full weeks. Calculate the integer remainder after dividing the number of days by 7.

How can I get the difference between two dates in PHP?

How to calculate the difference between two dates in PHP ?.
PHP | date_diff() Function..
Program to find the number of days between two dates in PHP..
Return all dates between two dates in an array in PHP..
PHP | fopen( ) (Function open file or URL).
PHP | fread( ) Function..
PHP | fclose( ) Function..
PHP | fwrite( ) Function..

How can I calculate the number of months between two dates in PHP?

“find how many months between two dates php” Code Answer.
$d1 = new DateTime("2018-01-10 00:00:00");.
$d2 = new DateTime("2019-05-18 01:23:45");.
$interval = $d1->diff($d2);.
$diffInSeconds = $interval->s; //45..
$diffInMinutes = $interval->i; //23..
$diffInHours = $interval->h; //8..
$diffInDays = $interval->d; //21..