【发布时间】:2021-02-16 12:19:53
【问题描述】:
我制作了一个页面,其中包含基于时间的图像,因此它们会在特定时间发生变化。现在我得到了所有的工作和东西,但它没有在正确的时间显示正确的图像它应该是这样的:
- 早上 = 6.00/12.00
- 下午 = 12.00/18.00
- 晚上 = 18.00/00.00
- 晚上 = 00.00/6.00
但例如它现在显示的是 13.00 的夜晚。我在这里做错了什么?
php
<?php
date_default_timezone_set("Europe/Amsterdam");
function getTime() {
$time = date('h:i');
$style = "";
if ($time >= 6 && $time < 12) {
$style = $style . "morning";
define("TIME", "Goodmorning!");
}
elseif($time >= 12 && $time < 18){
$style = $style . "afternoon";
define("TIME", "Goodafternoon!");
}
elseif($time >= 18 && $time == 0){
$style = $style . "evening";
define("TIME", "Goodevening!");
}
else {
$style = $style . "night";
define("TIME", "Goodnight!");
}
echo $style;
}
getTime();
?>
html
<!DOCTYPE html>
<html lang="eng">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<link rel="stylesheet" href="Site.css">
<title>PHP time based</title>
</head>
<body>
<div class= "text">
<div id="Time" class="<?php getTime(); ?>">
<h1><?php echo TIME; ?></h1>
<h1><?php echo "it is now " . date("h:i"); ?></h1>
</div>
</div>
</body>
</html>
css
body{
padding: 0%;
margin: 0%;
color: white;
height: 700px;
}
.text{
margin-top: auto;
text-align: center;
top: 50%;
bottom: 50%;
}
.morning{
background: url("images/morning.png") no-repeat center center fixed;
height: 700px;
}
.afternoon{
background: url("images/afternoon.png") no-repeat center center fixed;
height: 700px;
}
.evening{
background: url("images/evening.png") no-repeat center center fixed;
height: 700px;
}
.night{
background: url("images/night.png") no-repeat center center fixed;
height: 700px;
}
所以它应该显示如下文本:
下午好,现在是 13.45
并根据时间有不同的图像和文字
【问题讨论】:
-
使用
date('G')而不是date('h:i') -
如果我使用它,图像仍然不会在正确的时间显示
-
您似乎混淆了两件事。您问题中的代码不显示日期。那么为什么分钟对您的
getTime功能很重要? -
我不明白你不明白什么:) 这是一个简化的例子:3v4l.org/GsDaH 分钟在你的函数中并不重要。没有什么能阻止您在别处打印当前时间。
-
很明显,提问者想要以所描述的格式获取时间,从中挖掘小时以执行一些逻辑并显示它。我们可以批评这段代码的问题,但如果你问我,它是没有意义的。这显然是一个初学者,他/她尽了最大努力去实现一些东西,却遗漏了一些细节。鼓励是他/她需要我们提供的。我们需要向他/她展示这段代码与预期的接近程度,而不是指出他/她不知道多少。我想,我们最终想要解决问题。
标签: php