PHP函数可根据每月的一天和一天的小时显示不同的图像

时间:2019-02-21 03:40:21

标签: php image function

我正在尝试根据一个月的一天和一天的小时显示图像。这是我在论坛上找到并经过调整以满足我的需求的php代码

       <?php
    $h = date('G'); //set variable $h to the hour of the day
    $d = date('d'); //set variable $d to the day of the month.
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
    // Adjust offset code if needed $h = $h-2;


    // 01 Calendar Day 
    if ($d = 01 && $h >= 4 && $h < 12) $img = 'img/s1.jpg'; //if day is 1st and it's between 4am and 12pm show day strength 1 image
    else if ($d == 01 && $h >= 12 && $h < 2) $img = 'img/c1.jpg'; //if day is 1st and it's between 12pm and 2am show evening condition 1 image
    else if ($d == 01 && $h >= 2 && $h < 4) $img = 'img/rest.jpg'; //if day is 1st and it's between 2am and 4am show rest image


    ?>
<img src="/<?php echo $img; ?>">

目标是创建一个月中所有31天的if / else语句,其中有一个早晨图像,一个晚上图像和一个在深夜显示为缓冲区的缓冲区图像。

但是当我检查代码以查看它是否有效时,我得到了错误。请帮助,如果还有一种更有效的方法来编码此第31节的if / else语句,那也将很棒。

3 个答案:

答案 0 :(得分:1)

您无需应用日间条件,因为您的图片不会随着日变化。它随时间而变化。因此,您可以使用类似以下的内容:

$h = date('H'); // it will return hour in 24 format.


if ($h >= 4 && $h < 12) $img = 'img/s1.jpg'; //if it's between 4am and 12pm show day strength 1 image
    else if ( ($h >= 12 && $h <= 23) || ($h >= 1 && $h <= 2)  ) $img = 'img/c1.jpg'; //it's between 12pm and 2am show evening condition 1 image
    else if ($h >= 2 && $h < 4) $img = 'img/rest.jpg'; //if it's between 2am and 4am show rest image

您可以根据需要在算术运算符中进行更改。而且,如果您的图片以日为单位进行更改,则只需在图片变量中添加日期,如下所示:

$h = date('H'); // it will return hour in 24 format.
$d = date('d');     
    if ($h >= 4 && $h < 12) $img = 'img/s'.$d.'.jpg'; 
    else if ( ($h >= 12 && $h <= 23) || ($h >= 1 && $h <= 2)  ) $img = 'img/c'.$d.'.jpg'; 
    else if ($h >= 2 && $h < 4) $img = 'img/rest.jpg'; 

希望它对您有帮助。

答案 1 :(得分:0)

尝试一下

String extract = gson.toJson(dataFromApi);
Log.e("Extract", extract);

try {
      JSONObject jsonObject = new JSONObject(extract);
      jsonObject.get("images");
}
catch (JSONException e) {
      Log.e("ErrorParse resp to JSON", e.getMessage());
}

投放:

E/Extract: "{\"images\":[{\"lines\":[{\"text\":\"You remember things better\",\"words\":[{\"confidence\":\"high\",\"text\":\"You\"},{\"confidence\":\"high\",\"text\":\"remember\"},{\"confidence\":\"high\",\"text\":\"things\"},{\"confidence\":\"low\",\"text\":\"better\"}]},{\"text\":\"when you write them down\",\"words\":[{\"confidence\":\"high\",\"text\":\"when\"},{\"confidence\":\"high\",\"text\":\"you\"},{\"confidence\":\"high\",\"text\":\"write\"},{\"confidence\":\"high\",\"text\":\"them\"},{\"confidence\":\"high\",\"text\":\"down\"}]},{\"text\":\"BY\",\"words\":[{\"confidence\":\"high\",\"text\":\"BY\"}]},{\"text\":\"hand .\",\"words\":[{\"confidence\":\"high\",\"text\":\"hand\"},{\"confidence\":\"high\",\"text\":\".\"}]},{\"text\":\"Here\u0027s why.\",\"words\":[{\"confidence\":\"low\",\"text\":\"Here\u0027s\"},{\"confidence\":\"high\",\"text\":\"why.\"}]}],\"status\":\"Succeeded\"}]}"
E/ErrorParse resp to JSON: Value {"images":[{"lines":[{"text":"You remember things better","words":[{"confidence":"high","text":"You"},{"confidence":"high","text":"remember"},{"confidence":"high","text":"things"},{"confidence":"low","text":"better"}]},{"text":"when you write them down","words":[{"confidence":"high","text":"when"},{"confidence":"high","text":"you"},{"confidence":"high","text":"write"},{"confidence":"high","text":"them"},{"confidence":"high","text":"down"}]},{"text":"BY","words":[{"confidence":"high","text":"BY"}]},{"text":"hand .","words":[{"confidence":"high","text":"hand"},{"confidence":"high","text":"."}]},{"text":"Here's why.","words":[{"confidence":"low","text":"Here's"},{"confidence":"high","text":"why."}]}],"status":"Succeeded"}]} of type java.lang.String cannot be converted to JSONObject

答案 2 :(得分:0)

您可以将所有规则放入数组中,而不是多个if / else,以便于维护。

$array = [
    1 => [
        4 => 'img/s1.jpg',
        12 => 'img/s12.jpg',
        14 => 'img/s14.jpg',
    ],
    2 => [
        2 => 'img/c2.jpg'
    ]
];
function getDayDateImage($array, $d, $h)
{
    while (!$array[$d][$h]) {
        $h--;
        if ($h < 0) {
            $h = 23;
            $d--;
        }
        if ($d < 1) {
            return 'img/rest.jpg'; // Default
        }
    }
    return $array[$d][$h];
}

echo getDayDateImage($array, 1, 1); // Get img/rest.jpg
echo getDayDateImage($array, 1, 13); // Get img/s12.jpg
echo getDayDateImage($array, 2, 1); // Get img/s14.jpg
echo getDayDateImage($array, 2, 4); // Get img/c2.jpg
//Just pass you $d and $h 
//getDayDateImage($array, $d, $h);

希望获得帮助。