Stephen
Stephen

Reputation: 89

PHP Date Not Functioning As Expected

I have tried for hours to figure this out and it's beaten me. Why does $currentDay return a number representing the day of the week? Can I not assign a variable to the date?

<?php
date_default_timezone_set("America/New_York");
$currentDay = date('l'); // dayname
$currentTime = date("H");
$preOpen = "We're Open";
$preClosed = "We're Closed";
if ($currentDay = "Thursday" && $currentTime >= "22") {
  echo $preClosed."<br>";
} elseif ($currentDay = "Friday") {
  echo $preClosed."<br>";
} elseif ($currentDay = "Saturday" && $currentTime >= "22") {
  echo $preClosed."<br>";
} else {echo $preOpen."<br>";
}
echo date('l')."<br>";
echo $currentDay
?>

Upvotes: 0

Views: 32

Answers (1)

jdaz
jdaz

Reputation: 6043

In your if statements, you need to use == to check equality instead of =.

With your current code, when you write ($currentDay = "Thursday" && $currentTime >= "22"), PHP is actually evaluating the value "Thursday" && $currentTime >= "22" as a boolean (which returns 1 right now, after 10 PM in New York) and assigning it to $currentDay.

Demo

Upvotes: 3

Related Questions