1

I am trying to calculate the difference between two dates by excluding Saturday and Sunday. The output should be 9 but i got 1 instead.

<?php

$date1= mktime(0, 0, 0, 12, 20, 2011);
$date2= mktime(0, 0, 0, 12, 30, 2011);
$day1= date("D",$date1);
$day2= date("D",$date2);


$i=0;

while ($date1<=$date2)
{


if ($day1 != "Sat" || $day2 !="Sat" || $day1 != "Sun" || $day2 !="Sun")
   {
    $i++;
   }

else
   {
   $i+0;
   }

   $date1=array(date("Y",$date1), date("n",$date1), date("d",$date1)+1);
   }
     $diff=$i;

    echo $diff;

 ?>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Cooper
  • 23
  • 4
  • you may be interested in reading this http://stackoverflow.com/questions/56568/performing-datetime-related-operations-in-php – Jeffrey04 Oct 04 '11 at 03:54
  • possible duplicate of [Calculate business days](http://stackoverflow.com/questions/336127/calculate-business-days) – Luke Stevenson Oct 04 '11 at 04:02
  • I think this is a duplicate of http://stackoverflow.com/questions/3352712/get-date-range-between-two-dates-excluding-weekends – vascowhite Oct 04 '11 at 04:07

1 Answers1

0

Seems as if the condition in your if statement is giving the problem.

  if ($day1 != "Sat" && $day2 !="Sat" && $day1 != "Sun" && $day2 !="Sun")
 {
  $i++;
 }

 else
 {
  $i+0;
  }

Switching || to &&.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192