我打算根据用户进入网站的时间显示四个问候图像。
$morning = "img/morning.png";
$afternoon = "img/afternoon.png";
$evening = "img/evening.png";
$night = "img/night.png";
我有一些条件语句将值分配给$ cover变量。当我测试时,条件语句不起作用。
date_default_timezone_set('Asia/Yangon');
$Hour = date('G');
if($Hour >= 5 && $Hour <= 11) {
$cover = $morning;
}elseif ($Hour >= 11 && $Hour <= 4) {
$cover = $afternoon;
}elseif ($Hour >= 4 && $Hour <= 9){
$cover = $evening;
}elseif ($Hour >= 9 && $Hour <= 4) {
$cover = $night;
}
图片标签
<img class="card-img-top" src="<?php echo $cover; ?>" alt="Not Available" >
答案 0 :(得分:2)
if($Hour >= 0 && $Hour <= 11) {
$cover = $morning;
}
elseif ($Hour > 11 && $Hour <= 16) {
$cover = $afternoon;
}
elseif ($Hour > 16 && $Hour <= 19){
$cover = $evening;
}
else{
$cover = $night;
}
以上代码将检查您从00:00到第二天24:00的时间。我修正了if-else语句,使它们在某种意义上更具时代感。 p>
G的24小时格式,一个小时,前导零从0到23
答案 1 :(得分:0)
$hour = date('H', time());
if( $hour > 6 && $hour <= 11) {
$cover = $morning;
}
else if($hour > 11 && $hour <= 16) {
$cover = $afternoon;
}
else if($hour > 16 && $hour <= 23) {
$cover = $evening;
}
else {
$cover = $night;
}
构造日期实例并从中获取小时