gpt4 book ai didi

php - Else If 在 PHP 中有时间限制

转载 作者:搜寻专家 更新时间:2023-10-31 21:19:32 25 4
gpt4 key购买 nike

我正在尝试让我正在处理的这个网站根据一天中的不同时间显示不同的图片。我正在使用 PHP elseif 语句。

我试过将我的时间格式更改为“G”以关闭前导零,我试过使用整个一天的“H:i:s”,我试过只使用'H' 获取触发事件的时间。

<?php
date_default_timezone_set('America/New_York');
$datetime = new DateTime('now');
$time = $datetime->format('H');
if ($time <= 06 &&$time >= 20) {
echo '<img src="images/SpringNight.png" class="rounded img-fluid" width="720" height="480">';
}else{
echo '<img src="images/Spring.png" class="rounded img-fluid" width="720" height="480">';
}
?>

我试图让 elseif 中的第一个语句在晚上 8 点到早上 6 点之间弹出。整天就是输出else。

编辑:我想我明白了。我尝试使用“||”代替“&&”,它似乎奏效了。

<?php
date_default_timezone_set('America/New_York');
$datetime = new DateTime('now');
$time = $datetime->format('H');
if ($time <= 06 || $time >= 20) {
echo '<img src="images/SpringNight.png" class="rounded img-fluid" width="720" height="480">';
}else{
echo '<img src="images/Spring.png" class="rounded img-fluid" width="720" height="480">';
}
?>

最佳答案

我建议做一个|| (正如你在评论中所说)作为三元。然后您可以在没有回显的情况下打印您的 HTML,使用 $fileName 变量,而不必将所有 HTML 重复两次。

<?php
date_default_timezone_set('America/New_York');
$datetime = new DateTime('now');
$time = $datetime->format('H');
$fileName = ($time <= 6 || $time >= 20) ? 'SpringNight' : 'Spring';
?>
<img src="images/<?=$fileName?>.png" class="rounded img-fluid" width="720" height="480">

关于php - Else If 在 PHP 中有时间限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com