【问题标题】:Show message before and after certain time在特定时间之前和之后显示消息
【发布时间】:2016-06-03 14:26:18
【问题描述】:

目前我正在一家网上商店工作,它在同一天 17:00 之前发送它的订单。如果是在 17:00 之前、17:00 之后和周末,我想在成功或感谢页面上放置一条消息。所以三个消息。

我现在用 PHP 试试这个:

<?php
            //Show message
            $before_17 = "Order received before 17:00, will ship today.";
            $after_17="Order placed after 17:00, will ship tomorrow.";
            $weekend = "It's weekend, order will be shipped on Monday.";

            //Time & Day
            $current_time = date(H);
            $current_day = date(D);

            //Saturday
            if ($current_day == Sat){
            echo $weekend;
            }

            //Sunday
            elseif ($current_day == Sun){
            echo $weekend;
            }

            //Weekdays > 17
            elseif ($current_time > 17){
            echo $after_17;
            }

            //Weekdays < 17
            elseif($current_time < 17){
            echo $before_17;
            }

            else{
            echo 'Niks';
            }
        ?>

我希望这可以解决问题,但页面在 17:00 之后仍然显示 $before_17。难道我做错了什么?

【问题讨论】:

  • 您确定服务器设置为您期望的时区和夏令时吗?

标签: php date time


【解决方案1】:

编辑; 我刚刚注意到另一个问题。您应该使用if ($current_time &gt;= 17)(不只是大于),否则它将在小时为 18 之前生效。

您还应该在date() 中使用引号!尝试使用date('H')date('D') 而不是date(H)date(D)。并在当天比较中引用if ($current_day === 'Sun')

否则,您应该echo date('H'); 以确保服务器上的时间设置正确。

【讨论】:

  • 感谢您的编辑。这就是为什么我的 after_17 动作只会在正确的时间触发,如果我输入 16 作为小时,我猜。
  • 我也可以用小时和分钟来做这件事,所以如果它是 17.01 和 before_17 直到 17.00 或其他什么,我会触发 after_17?
  • 是的,我想你应该可以做$current_time = date('H.i'); 然后if($current_time &gt;= 17.01) 作为测试。但是我认为如果您使用&gt;= 规定大于或等于17,则只需使用小时即可。
【解决方案2】:

1 - 在 date() 中使用引号。喜欢date('D')

2- 在 if 中比较字符串时使用引号。喜欢if($current_day=='Sun')

【讨论】:

    【解决方案3】:

    这可能与时区问题有关

    我遇到了同样的错误($current_time 是 16,尽管它应该是 17)

    查看此link 了解更多信息

    【讨论】:

    • 所以我已将 set_timeline 代码添加到我的 php.ini 文件中。它现在工作得更好,但在 after_17 中我必须输入“16”作为使其正常工作的时间。这就是它的工作原理吗?
    • 你的意思是setTimezone()?您还需要使用echo date('H:i'); 实际检查服务器时间以进行检查。可能是服务器上的时间不正确,这种情况下setTimezone() 也会不正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    相关资源
    最近更新 更多