Function for selecting next Day If the last day is weekend

117 Views Asked by At

I am trying to create a function that checks the range of date. If the last date is an weekend it will select the next day. Suppose, Today is 1 March 2016. If someone add 5 with it, it will make the result 7 March 2016. Because 6 March is Weekend. I have written this code. But it is not working and I cannot find where is the problem. The code is as below:

<?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

There are 3 best solutions below

5
On BEST ANSWER

Check this:

  <?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;
?>
3
On

March 6th, 2016 is a Sunday, but you're checking for Friday and Saturday, you need to check for Saturday and Sunday instead, for the weekend. Also, your variable names need to match, and for Saturday you'll need to add two days to get the next weekday.

<?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);
?>
0
On

@Fakhruddin Ujjainwala I have changed in your code and it works now perfect. Thanks. the code is now as below:

<?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;
?>