我正在尝试制作一个代码,该代码将根据时间和日期显示两个不同的图像。
我希望它显示一个在周一至周五之间 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"(星期五)。
一个根本的缺陷是你应该使用==
(测试两个值是否相等)而不是=
(分配一个值)。但是大多数代码都很麻烦,整个例程可以用更紧凑的方式编写。试试:
$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";
仅此而已:)
如果实际上属于许多不同的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; ?>">
你使用=
分配,当你应该使用==
哪个比较。
您的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; ?>">
我会围绕以下内容进行一些事情:
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;
}
恕我直言,非常简单明了