【问题标题】:php time based image wont change rightphp基于时间的图像不会改变
【发布时间】: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


【解决方案1】:

这里和那里的一些修复。首先,您获得时间的方式,下午时间显示为 04:59 而不是 16:59,所以它是 AM/PM。这造成了一个重大问题。此外,逻辑也存在问题。总体而言,您非常接近解决方案,只是遗漏了一些小细节。这对我有用:

 <?php
      date_default_timezone_set("Europe/Amsterdam");

    function getTime() {


        $rawTime = explode(" ", date('h:i A'));
        $time = $rawTime[0];
        if (($rawTime[1] === "PM") && ($time !== "00:00")) {
            $temp = explode(":", $time);
            $temp[0] = ((int)$temp[0]) + 12;
            $time = implode(":", $temp);
        }
        $style = "";
    
        if (($time >= "06:00") && ($time < "12:00")) {
            $style = $style . "morning";
            define("TIME", "Goodmorning!");
        }
        else if(($time >= "12:00") && ($time < "18:00")){
            $style = $style . "afternoon";
            define("TIME", "Goodafternoon!");
        }
        else if(($time >= "18:00") && ($time < "24:00")){
            $style = $style . "evening";
            define("TIME", "Goodevening!");
        }
        else {
            $style = $style . "night";
            define("TIME", "Goodnight!");
        }
    
        echo "Good {$style}, it is now {$time}";
        return $time;
    }
    
    getTime();

【讨论】:

  • 非常感谢您对此进行解释并举例说明,非常感谢
  • 要添加不同的图像,我需要使用 HTML 吗?只需制作一个包含 4 个不同图像 src 的 div,然后在它们之间切换
  • @ptrique 欢迎您!是的,你需要做一些 HTML 的东西才能在浏览器中正确显示它。我建议将你的表示和计算的逻辑分开,这将有助于你编写更多通用、可移植和可重用的代码。暂时我并没有让我的回答过于复杂,因为第一件事是确保你在当前的努力中取得了一些成功。但是,在研究了不起作用的代码和起作用的代码之间的区别之后,也许重构是个好主意。
  • 对不起,如果我打扰了你,但我不知道如何在 html 中制作它,所以图像会改变。我制作了一个包含 4 个图像的 div,但它现在显示了所有 4 个图像,这是合乎逻辑的,因为它们都在 div 中,但我不知道如何使它工作,所以它一次只显示 1 个。我刚开始 php 和在这一点上我真的没有太多了解
  • 好吧,我测试了一下,我错了,"18:45" &gt; "18:15" 返回TRUE,因为4 在ASCII 表中比1 更远。我仍然认为不应该这样做,但这只是个人意见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 1970-01-01
  • 2020-01-11
相关资源
最近更新 更多