我一直试图想出一些可以根据服务器的时间改变图像的东西;这是一个实时聊天,我需要根据我们是否在线来改变图像。
这是我到目前为止所提出的:
<?php
$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week
$m = date('i'); //set variable $m to minute of the hour
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
if ($d >= 1 || $d <=5 && ($h == 9 || ($h == 8 && $m >= 30))) && ($h < 17 || ($h == 17 && $m <= 30))
{
$img = '{{media url="wysiwyg/live-chat.gif"}}';
}
else
{
$img = '{{media url="wysiwyg/Livechat-offline.gif"}}';
}
?>
我不太确定这是否正确,有人可以“批准”这个吗?
非常感谢任何帮助!
答案 0 :(得分:1)
您可以使用strtotime检查当前时间是否在您的营业时间之间。 像这样:
<?php
if(strtotime("now") < strtotime('date1') && strtotime("now") > strtotime('date2')){
// do your thing
}
else{
// do something else
}
?>
答案 1 :(得分:1)
您可以随时使用“更智能”的逻辑:
$wday = date('w');
$time = date('Hi');
if (($wday > 1) && ($wday <= 6)) { // if day is between monday to friday
if (($time > 0830) && ($time < 1730)) { // if time is between 8.30 and 17.30
$img = '{{media url="wysiwyg/live-chat.gif"}}';
}
}
// if $img is not set then it means it's not between the desired time-interval, meaning you are offline.
if (!isset($img)) $img = '{{media url="wysiwyg/Livechat-offline.gif"}}';
所以,在我们检查“从星期一@ 8.30到星期五@ 17.30”之前,如果我们在“星期四@ 2.00am”尝试它,它将显示为在线(因为你在时间范围内)。 /> 现在我们在检查我们是否在日间范围内时,独立于当天检查时间。