user5878053
user5878053

Reputation:

How to find Tomorrow day is "saturday" or not in PHP?

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.";

Upvotes: 0

Views: 1988

Answers (4)

Robert
Robert

Reputation: 20286

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");

Upvotes: 0

Ramalingam Perumal
Ramalingam Perumal

Reputation: 1427

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

Upvotes: 3

Brijal Savaliya
Brijal Savaliya

Reputation: 1091

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.";

Upvotes: 3

RJParikh
RJParikh

Reputation: 4166

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.";

Upvotes: 0

Related Questions