I';我的PHP有问题;s date()函数.具体是一周中的哪一天


I'm having trouble with PHP's date() function. Specifically which day of the week it is

我正试图为当地一家小型企业建立一个网站。我现在想做的是根据时间弹出一条小警报消息,显示企业是开业、即将关闭还是关闭。

我为公司的开业、停业和"最后一次通话"时间,以及一周中的今天和当前时间设置了一些变量。

然后我设置了两个if elseif语句。第一个选项将变量$status设置为1、2或3,第二个选项根据$status显示不同的HTML位。

这是我的代码:

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

这不是该代码的第一次迭代。我尝试过几种不同的结构,包括将两种不同的if elseifs组合成一种。我还写了一些回声,看看它卡在哪里。我的$today变量似乎总是认为是星期天,但不知何故,它使$status=1。

您在if语句中使用的是'=',而不是'===':)

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