Working days (Mon-Fri) in PHP

I have found this buggy when needing a larger amount of weekdays. I was looking for X amount of business days after the 1st of the current month.

Looked great at first until after adding > 5 business days (similar to what @zerkms found).

This has proved more accurate for me.

    function _getBusinessDayOfMonth( $days ) { 
        $time = strtotime(date("m/1/Y 00:00")); //finding # of business days after 1st of the month
        $i = 0; //start with zero
        while ($i < $days) { //loop through until reached the amount of weekdays
            $time = strtotime("+1 day", $time); //Increase day by 1
            if (date("N", $time) < 6) { //test if M-F
                $i++; //Increase by 1
            }
        }
        echo date("m/d/Y", $time);
    }

If you are limiting to weekdays use the string weekdays.

echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) );

This should jump you ahead by 3 weekdays, so if it is Thursday it will add the additional weekend time.

Source: http://www.php.net/manual/en/datetime.formats.relative.php

Tags:

Php

Strtotime