简体   繁体   English

PHP-基于时间的不同状态

[英]PHP - Different states based on time

i'm currently working on a project where business are opened and closed in real time by using image sprite representations of them. 我目前正在从事一个项目,该项目使用它们的图像精灵表示来实时打开和关闭业务。

In the code below i'm setting the default timezone to GMT and the time format to 0000. When testing this is fine except for between 0000 and 0959. It appears to be ignoring the time format and not putting the leading zeros in. 在下面的代码中,我将默认时区设置为GMT,将时间格式设置为0000。进行测试时,除0000和0959之间的值外,其他都很好。它似乎忽略了时间格式,而不输入前导零。

Additionally, is there a more efficient way of doing the below? 此外,还有以下更有效的方法吗?

Thanks in advance. 提前致谢。

<?php date_default_timezone_set("Europe/London"); $time = date("Hi");?> 

.<?php if($time > 1600 && $time < 2329){echo "business1";} else{echo "business1:hover";}?> {background: url(http://website.com/business1.png) 0 -145px;}
.<?php if($time > 1600 && $time < 2329){echo "business1:hover";} else{echo "business1";}?> {background: url(http://website.com/business1.png) 0 0;}

.<?php if($time > 0800 && $time < 1659){echo "business2";} else{echo "business2:hover";}?> {background: url(http://website.com/business2.png) 0 -145px;}
.<?php if($time > 0800 && $time < 1659){echo "business2:hover";} else{echo "business2";}?> {background: url(http://website.com/business2.png) 0 0;}

A literal 0800 in PHP is understood to be an octal number due to the leading 0 . 由于前导0 ,PHP中的文字0800 被理解为八进制 You should not compare times as strings to begin with. 您不应该将时间作为字符串进行比较。 Use UNIX timestamps for time comparison: 使用UNIX时间戳进行时间比较:

$now = time();
if ($now > strtotime('16:00') ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM