选择下一天的功能如果最后一天是周末

时间:2016-02-28 19:35:28

标签: php date datetime strtotime weekend

我正在尝试创建一个检查日期范围的函数。如果最后一个日期是周末,它将选择第二天。假设,今天是2016年3月1日。如果有人加5,它将在2016年3月7日结果。因为3月6日是周末。我写了这段代码。但它不起作用,我找不到问题所在。代码如下:

<?php
$date = "2016-02-29";
$numOfDays = 8;
$futureDate = strtolower(date("l",strtotime($date." +".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";

        if($futureDate != $$w1 || $futureDate != $w2){
            $finalDate = date("Y-m-d",strtotime($futureDate));
        }
        else{$finalDate = date("Y-m-d",strtotime($futureDate ."+1 day"));}

        echo $finalDate;
?>

3 个答案:

答案 0 :(得分:1)

2016年3月6日是一个星期天,但是你要检查星期五和星期六,你需要检查周六和周日,而不是周末。此外,您的变量名称需要匹配,并且对于星期六,您需要添加两天才能获得下一个工作日。

<?php
$date = "2016-02-29";
$numOfDays = 8;
$day = 86400;//one day is 86400 seconds
$time = strtotime($futureDate + $numOfDays * $day);//converts $futureDate to a UNIX timestamp
$futureDate = strtolower(date("l", $time));
$saturday = "saturday";
$sunday = "sunday";
if($futureDate == $saturday){
    $finalDate = date("Y-m-d", $time + 2 * $day);
}
else if($futureDate == $sunday){
    $finalDate = date("Y-m-d", $time + $day);
}
else{
    $finalDate = date("Y-m-d", $time);
}
echo($finalDate);
?>

答案 1 :(得分:1)

检查一下:

  <?php
$date = "2016-02-29";
$numOfDays = 11;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";

if($futureDate == $weekend1){
      $finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
if ($futureDate == $weekend2){
      $finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}

echo $finalDate1;
?>

答案 2 :(得分:0)

@Fakhruddin Ujjainwala 我已经改变了你的代码,现在它完美无缺。谢谢。代码现在如下:

<?php
$date = "2016-02-29";
$numOfDays = 4;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";

if($futureDate == $weekend1){
      $finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
else if ($futureDate == $weekend2){
      $finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
else{
    $finalDate1 = date("Y-m-d",strtotime($futureDate1));
}

echo $finalDate1;
?>