Ashley Stewart
Ashley Stewart

Reputation: 55

PHP Function to show different image based on day of the month and hour of the day

I'm trying to display an image based on the day of the month and the hour of the day. Here's the php code that I found via a forum and tweaked to meet my needs

       <?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; ?>">

The goal is to create if/else statements for all 31 possible days in a month, where there is a morning image, an evening image, and a buffer image that displays in the late night as a buffer.

But when i check the code to see if it works I'm getting errors. Please help and also if there's a more efficient way to code this verses 31 if/else statements that would be wonderful also.

Upvotes: 0

Views: 865

Answers (3)

Instead of multiple if/else, you can put all the rule into array for easy to maintain.

$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);

Hope this help.

Upvotes: 0

M.Hemant
M.Hemant

Reputation: 2355

Try This,

$h = date('G'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
$d = date('d'); //set variable $d to the day of the month.
$DynamicDay = '21';
if ($DynamicDay == $d) {
    switch ($h) {
        case ($h >= 4 && $h < 12):
            $img = 'img/s' . $d . '.jpg';
            break;
        case ($h >= 12 && $h < 14):
            $img = 'img/c1.jpg';
            break;
        case ($h >= 14 && $h < 18):
            $img = 'img/rest.jpg';
            break;
        default:
            break;
    }
}
echo 'Hour => '.$h.'<p>';
echo 'day => '.$d.'<p>';
echo $img;
die;

Out Put:

Hour => 10

day => 21

img/s21.jpg

Upvotes: 0

Rohit Mittal
Rohit Mittal

Reputation: 2104

You dont need to apply day condition as your image is not changing bases on day. It is changing on time bases. So you can use something like below:

$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

You can make changes in your arithmetical operators as per your requirement. And if your image changes on day bases, you can simply add date in your image variable as below:

$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'; 

Hope it helps you.

Upvotes: 1

Related Questions