我正在尝试创建一个代码,根据时间和日期显示两个不同的图像。
我想让它在星期一到星期五之间的7:25 - 12:40和13:30 - 14:10之间显示一个“开放”的图像。在周末和任何其他时间,它应该显示图像“关闭”。
这是我一直试图开始工作的代码。
<?php
date_default_timezone_set('Europe/Copenhagen');
$h = date('Gi'); //G (timer) = 0 til 23 og i (minutter) = 00 til 59
$d = date('N'); //1 (for mandag) til 7 (for søndag)
// MANDAG
if ($d = '1' && $h >= 745 && $h < 1240) $img = 'images/open.png';
if ($d = '1' && $h >= 1330 && $h < 1410) $img = 'images/open_red.png';
// TIRSDAG
if ($d = '2' && $h >= 745 && $h < 1240) $img = 'images/open.png';
if ($d = '2' && $h >= 1330 && $h < 1410) $img = 'images/open_red.png';
// ONSDAG
if ($d = '3' && $h >= 745 && $h < 1240) $img = 'images/open.png';
if ($d = '3' && $h >= 1330 && $h < 1410) $img = 'images/open_red.png';
// TORSDAG
if ($d = 4 && $h >= 745 && $h < 1240) $img = 'images/open.png';
if ($d = 4 && $h >= 1330 && $h < 1410) $img = 'images/open_red.png';
// FREDAG
if ($d = 5 && $h >= 745 && $h < 1240) $img = 'images/open.png';
// LØRDAG
// SØNDAG
else $img = 'images/closed.png';
?>
<img src="<?php echo $img; ?>">
出于某种原因,它忽略了day变量,只打印出最后一个条目,即“fredag”(星期五)。
答案 0 :(得分:1)
一个根本的缺陷是,您应该使用==
(如果两个值相等,则测试)而不是=
(指定 a值)。但是大多数代码都很麻烦,整个例程可以用更紧凑的方式编写。试试吧:
$h = date('Gi');
$d = date('N');
$img = "images/closed.png";
if ( ($d >= 1 && $d <= 6) && ($h >= 745 && $h < 1240) ) $img = "images/open.png";
elseif ( ($d >= 1 && $d <= 5) && ($h >= 1330 && $h < 1410) ) $img = "images/open_red.png";
这就是全部:)
答案 1 :(得分:0)
如果实际上属于多个不同的if
分支,但每次连续覆盖$img
。
您的主要问题是您没有将$d
与事物进行比较,而是每次都分配$d
个值。为了进行比较,您需要双等号:==
,如if ($d == 5 && ...
此外,您可以考虑使用if,else if,else梯形图而不是if语句。就目前而言,如果第一个if
语句是“正确”语句,$img
仍将设置为closed.png
,因为它失败了最后一个if
语句。
如果您正确使用了if,else if,else梯形图,则不会发生这种情况(并且您的脚本会运行得更快)。
TLDR: 这应该有效:
$h = date('Gi'); //G (timer) = 0 til 23 og i (minutter) = 00 til 59
$d = date('N'); //1 (for mandag) til 7 (for søndag)
// MANDAG
if ($d == 1 && $h >= 745 && $h < 1240){ $img = 'images/open.png'; }
elseif ($d == 1 && $h >= 1330 && $h < 1410){ $img = 'images/open_red.png'; }
// TIRSDAG
elseif ($d == 2 && $h >= 745 && $h < 1240){ $img = 'images/open.png'; }
elseif ($d == 2 && $h >= 1330 && $h < 1410){ $img = 'images/open_red.png'; }
// ONSDAG
elseif ($d == 3 && $h >= 745 && $h < 1240){ $img = 'images/open.png'; }
elseif ($d == 3 && $h >= 1330 && $h < 1410){ $img = 'images/open_red.png'; }
// TORSDAG
elseif ($d == 4 && $h >= 745 && $h < 1240){ $img = 'images/open.png'; }
elseif ($d == 4 && $h >= 1330 && $h < 1410){ $img = 'images/open_red.png'; }
// FREDAG
elseif ($d == 5 && $h >= 745 && $h < 1240){ $img = 'images/open.png'; }
// LØRDAG
// SØNDAG
else{ $img = 'images/closed.png'; }
?>
<img src="<?php echo $img; ?>">
答案 2 :(得分:0)
当您使用=
进行比较时,您使用==
进行分配。
您的else
也会附加到上一个if
,因此您只会在周五看到“打开”图片。使用大括号使if / elseif / else结构更清晰。
更好的方法是默认情况下使其“关闭”,然后使用if / else结构(带括号,总是带括号!),如下所示:
$h = date('Gi'); //G (timer) = 0 til 23 og i (minutter) = 00 til 59
$d = date('N'); //1 (for mandag) til 7 (for søndag)
$img = 'images/closed.png'; // By default the image is "closed"
if ($d < 6) // If Monday to Friday
{
if ($h >= 745 and $h < 1240) // If early
{
$img = 'images/open.png'; // then it's open
}
elseif ($h >= 1330 and $h < 1410 and $day != 5) // If late, and NOT Friday
{
$img = 'images/open_red.png'; // then it's red open
}
}
?>
<img src="<?php echo $img; ?>">
答案 3 :(得分:0)
我会寻找一些与此相关的内容:
list($day_of_week, $hour_minute) = explode(' ', date('N Gi'));
switch( $day_of_week ) {
case '7':
case '6':
# weekend days, always closed:
$image = 'closed.png';
break;
default:
# weekdays, so check the time instead:
if ( ( (int)$hour_minute >= 745 && (int)$hour_minute <= 1240 ) || ( (int)$hour_minute >= 1330 && (int)$hour_minute <= 1410 ) ) {
$image = 'open.png';
} else {
$image = 'closed.png';
}
break;
}
非常直接且易于阅读imho