开发者

PHP: E-leave: Calculating two dates using if statement in a while loop

开发者 https://www.devze.com 2023-04-10 04:27 出处:网络
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.

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;

 ?>


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 &&.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号