Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于php时间的图像不会正确更改_Php - Fatal编程技术网

基于php时间的图像不会正确更改

基于php时间的图像不会正确更改,php,Php,我用基于时间的图像制作了一个页面,以便它们在特定时间发生变化。现在我让它工作了,但是它没有在正确的时间显示正确的图像,应该是这样的: 上午=6.00/12.00 下午=12.00/18.00 晚上=18.00/00.00 夜间=00.00/6.00 但举例来说,它显示的是现在13点的夜晚。我做错了什么 php <?php date_default_timezone_set("Europe/Amsterdam"); function

我用基于时间的图像制作了一个页面,以便它们在特定时间发生变化。现在我让它工作了,但是它没有在正确的时间显示正确的图像,应该是这样的:

  • 上午=6.00/12.00
  • 下午=12.00/18.00
  • 晚上=18.00/00.00
  • 夜间=00.00/6.00
但举例来说,它显示的是现在13点的夜晚。我做错了什么

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();
    ?>
因此,它应该显示如下文本:

下午好,现在是13点45分


并且根据时间有不同的图像和文本

这里和那里有一些修正。首先,按照你计算时间的方式,下午时间显示为04:59而不是16:59,所以是上午/下午。这造成了一个大问题。此外,逻辑也有问题。总的来说,您非常接近解决方案,只是错过了一些小细节。这对我很有用:

 <?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();

使用
date('G')
而不是
date('h:i')
如果我使用的图像仍然无法在正确的时间显示,那么您似乎混淆了两件事。问题中的代码没有显示日期。那么,为什么分钟对你的
getTime
函数很重要呢?我不明白你不明白的是什么:)这里有一个简单的例子:在你的函数中,分钟并不重要。没有什么能阻止你在其他地方打印当前时间。很明显,询问者希望以所描述的格式获取时间,从中挖掘小时以执行一些逻辑并显示它。我们可以批评这段代码存在的问题,但如果你问我的话,它是没有意义的。这显然是一个初学者,他/她尽了最大的努力去实现一些东西,却错过了一些细节。他/她需要我们的鼓励。我们需要向他/她展示此代码与预期的代码有多接近,而不是指出他/她不知道多少。我们最终想解决问题,我想。非常感谢你解释这一点,并展示了一个例子,真的很感激它添加不同的图像,我需要使用HTML吗?只需用4个不同的图像制作一个div src right并在它们之间切换即可them@ptrique不客气!是的,为了在浏览器中正确显示,您需要执行一些HTML操作。我建议将表示和计算逻辑分开,这将有助于编写更多通用、可移植和可重用的代码。目前,我没有将我的答案过于复杂化,因为首先要确保你目前的努力取得一些成功。但是,在研究了不起作用的代码和起作用的代码之间的区别之后,也许重构是一个好主意。对不起,如果我打扰了你,但是我不知道如何在html中进行重构,因此图像会发生变化。我制作了一个包含4个图像的div,但它现在显示了所有4个图像,这是合乎逻辑的,因为它们都在div中,但我不知道如何使它工作,所以一次只显示1个。我刚刚启动php,在这一点上我真的没有得到太多,我测试了它,我错了,
“18:45”>“18:15”
返回
TRUE
,因为
4
在ASCII表中比
1
更靠前。我仍然认为人们不应该这样做,但这只是个人观点。
  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;
    }
 <?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();