• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Get all work days in a year

Hi experts,

I need to make a record in my MySQL database for every work day in a year. Hollydays are not important in this script as long as it does not include sundays or saterdays.

I found this script (see below) but it does not work.

(script found : http://www.bitsntricks.com/php-script-how-to-find-the-working-days-from-a-specified-date/)

It gives me an error;

Parse error: syntax error, unexpected T_STRING in \workdays.php on line 39

Can someone please help me acceive my goal?



echo workingDays( "05-01-2011", "10");


This is a function to find out working days from a specified date and  format of date must be dd-mm-yyyy

First parameter to function is the date from which working days are to be calculated (working day exclude Sun and Sat)

Second parameter to function is the number of working days to be calculated


function workingDays( $fromDate, $interval )  {

$date_array = explode(‘-’, $fromDate );

$day                      = $date_array[0];

$month                                = $date_array[1];

$year                     = $date_array[2];

$working_date = array();

for ( $i = 1;  $i <= $interval; $i++ )               {

$day_text = date(“D”, mktime( 0, 0, 0,$month,$day + (int)$i,$year));

if( $day_text == ‘Sat’ || $day_text == ‘Sun’ )  {




$working_date[] = date(“F j, Y”, mktime(0, 0, 0,$month,$day +(int)$i,$year));


return $working_date;


$getWorkingDays = workingDays(’19-05-2010', 10 );

echo “<pre>————————–Array with all working days ————————————–<br/>”;


echo “——————————- Last working day—————————————————<br/>”;

echo $getWorkingDays[count($getWorkingDays)-1];


Open in new window

1 Solution
Try this on line 39:

$working_date[] = date('F j, Y', mktime(0, 0, 0,$month,$day +(int)$i,$year));

Open in new window

Beverley PortlockCommented:
Try this code....


     function getWorkingDays( $y ) {

          $arr = array();

          $testDate = "$y-01-01";

          for( $i=0; $i < 366; $i++ ) {

               // See if we are in next year
               if ( date("Y", strtotime($testDate) ) != $y )

               // Check what the day is. If not a weekend then
               // put in the results array
               $day = date("D", strtotime($testDate) );
               if ( $day != "Sat" && $day != "Sun" )
                    $arr [] = $testDate;

               // Advance to the next day
               $testDate = date("Y-m-d", strtotime("$testDate +1 day") );

          return $arr;

$year = 2011;
print_r( getWorkingDays( $year ) );

Open in new window

SteynskAuthor Commented:
All quotes were wrong....
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now