0

This is my code. I am using the strtotime function for find the next day is saturday. But my code result is always run else part. Where I am wrong.

$current_date = date('Y-m-d');

$date = strtotime($current_date); 

$weekDay = date('w', strtotime('+1 day',$date));

if(($weekDay == 'Saturday'))
    echo "Tomorrow is Saturday.";
else
    echo "Tomorrow is not Saturday.";
Shay Altman
  • 2,720
  • 1
  • 16
  • 20
  • `var_dump()` your variables to actually *see* what you're dealing with...! Also, `strtotime(date('Y-m-d'))` is the same as `time()`. – deceze Apr 15 '16 at 13:49
  • 1
    The `w` option for date returns 0-6, not a word. – j08691 Apr 15 '16 at 13:49
  • Possible duplicate of [Checking if date is weekend PHP](http://stackoverflow.com/questions/4802335/checking-if-date-is-weekend-php) – Tom Apr 15 '16 at 13:54

4 Answers4

3

Use 'l' instead "w"

$current_date = date('Y-m-d');

$date = strtotime($current_date); 

$weekDay = date('l', strtotime('+1 day',$date));
if(($weekDay == 'Saturday'))
    echo "Tomorrow is Saturday.";
else
    echo "Tomorrow is not Saturday.";
Brijal Savaliya
  • 1,101
  • 9
  • 19
3

It will give you 6 as output. You could change the "6" insteadof "saturday".

$date = time(); //Current date 
$weekDay = date('w', strtotime('+1 day',$date));

if(($weekDay == 6)) //Check if the day is saturday or not.
    echo "Tomorrow is saturday.";
else
    echo "Tomorrow is not saturday.";

Result :

Tomorrow is saturday.

NOTE :
0 => Sunday
1 => Monday
2 => Tuesday
3 => Wednesday
4 => Thursday
5 => Friday
6 => Saturday
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
0

It will give you 6 as output.

$current_date = date('Y-m-d');

$date = strtotime($current_date); 

$weekDay = date('w', strtotime('+1 day',$date));

if(($weekDay == 6))
    echo "Tomorrow is Saturday.";
else
    echo "Tomorrow is not Saturday.";
RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

There is very simple solution using w param in date() function

if (date('w', strtotime("+1 day")) == 6) {
 // tomorrow is saturday
}

or more objective solution

if ((new DateTime())->modify('+1 day')->format('w') == 6) {
   // tomorrow is saturday
}
  • w - returns day of week
  • 6 - represents saturday

To make it short you can use tenary operator

printf("Tomorrow is%s saturday", date('w', strtotime("+1 day")) == 6 ? '' : " not");
Robert
  • 19,800
  • 5
  • 55
  • 85