<p>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.</p>

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

[{'answer_id': 80553, 'author': 'erlando', 'author_id': 4192, 'author_profile': 'https://Stackoverflow.com/users/4192', 'pm_score': 0, 'selected': False, 'text': "<p>One way would be to convert the dates to unix timestamps using strtotime(...), subtracting the results and div'ing with 86400 (24*60*60):</p>\n\n<pre><code>$dif_in_seconds = abs(strtotime($a) - strtotime($b));\n$daysbetween = $dif_in_seconds / 86400;\n</code></pre>\n\n<p>ETA: Oh.. You meant weekdays as in Mon-Fri.. Didn't see that at first..</p>\n"}, {'answer_id': 80557, 'author': 'cruizer', 'author_id': 6441, 'author_profile': 'https://Stackoverflow.com/users/6441', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>The PHP functions mktime() and date() (for working with UNIX timestamps) are your friends here.</p>\n"}, {'answer_id': 80578, 'author': 'kobusb', 'author_id': 1620, 'author_profile': 'https://Stackoverflow.com/users/1620', 'pm_score': 3, 'selected': True, 'text': '<pre><code>        $datefrom = strtotime($datefrom, 0);\n        $dateto = strtotime($dateto, 0);\n\n        $difference = $dateto - $datefrom;\n\n        $days_difference = floor($difference / 86400);\n        $weeks_difference = floor($days_difference / 7); // Complete weeks\n\n        $first_day = date("w", $datefrom);\n        $days_remainder = floor($days_difference % 7);\n\n        $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?\n        if ($odd_days &gt; 7) { // Sunday\n            $days_remainder--;\n        }\n        if ($odd_days &gt; 6) { // Saturday\n            $days_remainder--;\n        }\n\n        $datediff = ($weeks_difference * 5) + $days_remainder;\n</code></pre>\n\n<p>From here: <a href="http://www.addedbytes.com/php/php-datediff-function/" rel="nofollow noreferrer">http://www.addedbytes.com/php/php-datediff-function/</a></p>\n'}, {'answer_id': 80627, 'author': 'Matej', 'author_id': 11457, 'author_profile': 'https://Stackoverflow.com/users/11457', 'pm_score': 2, 'selected': False, 'text': "<p>If you are creating an invoicing system, you have to think about the bank holidays, Easter, etc. It is not simple to compute it. </p>\n\n<p>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). </p>\n\n<p>You can find this solution fully described in <em>Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL</em></p>\n"}]
['https://Stackoverflow.com/questions/80541', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/131/']