Given two dates what is the best way of finding the number of weekdays in PHP?

StackOverflow https://stackoverflow.com/questions/80541

  •  09-06-2019
  •  | 
  •  

Question

The title is pretty much self explanatory. Given two dates what is the best way of finding the number of week days using PHP? Week days being Monday to Friday.

For instance, how would I find out that there are 10 week days in between 31/08/2008 and 13/09/2008?

Was it helpful?

Solution

        $datefrom = strtotime($datefrom, 0);
        $dateto = strtotime($dateto, 0);

        $difference = $dateto - $datefrom;

        $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;

From here: http://www.addedbytes.com/php/php-datediff-function/

OTHER TIPS

If you are creating an invoicing system, you have to think about the bank holidays, Easter, etc. It is not simple to compute it.

The best solution I have ever seen is to pregenerate a table with days and its type to SQL database (row per day = 365 rows per year) and then perform simple count query with proper selection (WHERE clause).

You can find this solution fully described in Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL

One way would be to convert the dates to unix timestamps using strtotime(...), subtracting the results and div'ing with 86400 (24*60*60):

$dif_in_seconds = abs(strtotime($a) - strtotime($b));
$daysbetween = $dif_in_seconds / 86400;

ETA: Oh.. You meant weekdays as in Mon-Fri.. Didn't see that at first..

The best way is to iterate through all dates in between the given date range, and get the day of week for each date. If it's a week day, increment a certain counter. At the end of the process you get the number of weekdays.

The PHP functions mktime() and date() (for working with UNIX timestamps) are your friends here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top