0

I'm attempting to build a website for a small local business. What I'm attempting to do right now is make a small alert message pop up based on times showing if the business is open, closing soon, or closed.

I've set up some variables for the business's opening, closing, and "last call" times as well as today's day of the week, and the current time.

Then I set up two if elseif statements. The first sets the variable $status to either 1, 2 or 3, and the second displays different bits of HTML based on $status.

Here's my code:

<?php

date_default_timezone_set("America/New_York");

$now_hour = date("H");
$now_min = date("i");
$today = date( "d" );
$open = 10;
$warn_hour = 18;
$warn_min = 30;
$close = 19;


if ( $today = 0 ) {
        $status = 0;
} elseif ( $now_hour < $open ) {
        $status = 0;                                                        
} elseif ( $now_hour > $close ) {
        $status = 0;
} elseif ( $now_hour > $warn_hour && $now_min > $warn_min ) {
        $status = 1;
} else {
        $status = 2;
};



if ( $status = 0 ) { ?>
    <div class="alert alert-danger" role="alert">Sorry! We're closed!</div>
<?php } elseif ($status = 1) { ?>
    <div class="alert alert-warning" role="alert">We're closing soon! Hurry up and get your orders in!</div>
<?php } elseif ($status = 2) { ?>
    <div class="alert alert-success" role="alert">We're open! Come enjoy a hot dog!</div>
<?php } else { ?>
    <div class="alert alert-info" role="alert">There's been an error. Check our hours to see if we're open.</div>
<?php }; ?>

This isn't the first iteration of this code. I've tried several different structures, including making the two different if elseifs into one. I've also written in some echos to see where it gets stuck. It seems that my $today variable ALWAYS thinks its Sunday and yet somehow makes $status = 1.

Andy
  • 5
  • 2
  • 4
    You also are using `=` for comparison, `=` is assignment, `==` is comparison – Mark Baker Dec 02 '15 at 13:27
  • To make your life easier for date based stuff, I would also suggest using [Carbon](https://github.com/briannesbitt/Carbon). As soon as you want to do date based calculations, that comes in very handy. – ciruvan Dec 02 '15 at 13:32

1 Answers1

1

You are using '=' in your if statements instead of '==' :)

<?php

date_default_timezone_set("America/New_York");

$now_hour = date("H");
$now_min = date("i");
$today = date( "d" );
$open = 10;
$warn_hour = 18;
$warn_min = 30;
$close = 19;


if ( $today == 0 ) {
    $status = 0;
} elseif ( $now_hour < $open ) {
    $status = 0;                                                        
} elseif ( $now_hour > $close ) {
    $status = 0;
} elseif ( $now_hour > $warn_hour && $now_min > $warn_min ) {
    $status = 1;
} else {
    $status = 2;
};



if ( $status == 0 ) { ?>
    <div class="alert alert-danger" role="alert">Sorry! We're closed!</div>
<?php } elseif ($status == 1) { ?>
<div class="alert alert-warning" role="alert">We're closing soon! Hurry up    and get your orders in!</div>
<?php } elseif ($status == 2) { ?>
<div class="alert alert-success" role="alert">We're open! Come enjoy a hot dog!</div>
<?php } else { ?>
<div class="alert alert-info" role="alert">There's been an error. Check our hours to see if we're open.</div>
<?php }; ?>
Thomas VB
  • 103
  • 8