tags:

views:

279

answers:

3

Hi Friends i am develop a webpage in that i need to calculate x days from a specified date , The problem is we exclude the saturday and sunday . For example $Shipdate = '06/30/2009' and the x is 5 means , i want the answer '7' [ie 30 is tuesday so after 5 days it will be sunday , so there is two holiday(saturday and sunday) so we add 5+2(for saturday and sunday)]=7. Please help me to find out , Thanks in advance.

A: 
<?php

// ** Set Beginning and Ending Dates, in YYYY-mm-dd format **
 $begin = '2008-01-01';
 $end = '2008-03-31';
 $begin_mk = strtotime("$begin"); 
 $end_mk = strtotime("$end");

// ** Calculate number of Calendar Days between the two dates
 $datediff = ( $end_mk > $begin_mk ? ( $end_mk - $begin_mk ) : ( $begin_mk - $end_mk ) );
 $days = ( ( $datediff / 3600 ) / 24 );
  $days = $days + 1; // to be inclusive of last date;

// ** Count days excluding Sundays **
  $iteration = 0; 
  $numDaysExSunday = 0;
for ($i=1; $i<=$days; $i++) {
  $weekday = date("w", strtotime("$begin + $iteration day"));
       echo "$weekday<br>";
     **// i change only this line to add saturday**
     if ($weekday !== '0' && $weekday !== '6') {
        $numDaysExSunday++;
     }
  $iteration++; 
}

// ** Output number of days excluding Sundays **
echo $numDaysExSunday;
?>

i take it from

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_23499410.html

the solution Excluding Sundays but need to change only one line to exclude saturday i write it in the code

Haim Evgi
You should rename the variable as well, and also include holidays. I would replace the weekday comparison with a function that returns whether it's a business day.
Drew Hoskins
+1  A: 

Generally you will need to be able to specify a calendar with significant days excluded. Consider Christmas Day or public holidays. This appears to be code that will consider public holidays, you need to modify it or parameterise it with your set of holidays.

djna
A: 
function Weekdays($start,$end){
    $days=0;
    if ( $end <= $start ) {
     // This is invalid data.
     // You may consider zero to be valid, up to you.
     return; // Or throw an error, whatever.
    }
    while($start<$end){
     $dayofweek = date('w',$start);
     if( 6!= $dayofweek && 0 != $dayofweek){
      $days++;
     }
     $start = strtotime('+1 day',$start);
    }
    return $days;
}

You may want to tweak it a bit depending on whether you want to count it as one day or zero if the start is the same day as the end.

Eli