仅在工作时间显示链接

仅在工作时间显示链接

本文介绍了(PHP) 仅在工作时间显示链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在网站上在工作时间内显示实时聊天链接.我有下面的代码,它似乎在下午工作,但在早上不起作用,我不知道为什么...... $start$end是从 MySQL 数据库接收的值,但在我的示例中,我对它们进行了硬编码以使示例更简单.

I am trying to make a live chat link appear on the website only during business hours. I have the code below which seems to work in the afternoon, but won't work in the morning and I'm not sure why... $start and $end are values received from a MySQL database but in my example I've hard coded them to make the example simpler.

$LinkStatus = "on";

$start = 9:00:00;
$end = 23:00:00;
$current_time = date('G:i:s'); //9:35:00

if (($start > $current_time) || ($end < $current_time)) {
    $LinkStatus = "off";
}

如果开始时间大于当前时间,则该业务尚未开放.如果结束时间小于当前时间,则为小时后.在上午 9 点到晚上 11 点(23:00)之间的任何时间,这些条件都不应该为真,因此 $LinkStatus 应该保持 "on".但是,它现在似乎没有这样做.有些东西将它设置为 "off".

If the start time is greater than the current time, then the business is not open yet. If the end time is less than the current time, then it's after hours. Any time between 9am and 11pm (23:00) neither one of those conditions should be true, therefore $LinkStatus should remain "on". However, it does not seem to be doing that right now. Something is setting it to "off".

我已经在 if 语句上方和下方回显了变量,所以我可以确认是这个 if 语句导致变量被设置为 "off".

I've echoed the variable above the if statement and below it so I can confirm it's this if statement causing the variable to be set to "off".

正如您可能从我的代码示例中看到的,我对 PHP 不是很了解.任何帮助表示赞赏.

As you can probably see from my code example, I'm not very knowledgeable when it comes to PHP. Any help is appreciated.

推荐答案

date('G:i:s')  // 24 hours time without leading zero for hour

...不会像字符串那样排序,例如 '9' > '10'.

...won't sort well as a string, for example '9' > '10'.

改为使用 24 小时制,并带有前导零,这使得正确的排序为 '09' <'10';

Use 24 hour time with a leading zero instead, which makes the correct sort '09' < '10';

date('H:i:s')  // 24 hour time with leading zero for hour

这篇关于(PHP) 仅在工作时间显示链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:52