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

displaying a different image depending on the time of the day
javascript change image based on time of day
javascript display images dynamically
php datetime first day of month
php get first day of month
javascript if statement display image
php last day of previous month
php get first day of week from week number

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.

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.

Relative Formats - Manual, This page describes the different relative date/time formats that the strtotime(), ' front of' hour, 15 minutes before the specified hour, "front of 5am", "front of 23" ' last day of', Sets the day to the last day of the current month. Relative month values are calculated based on the length of months that they pass through. I'm trying to make a code that will show two different images depending on what the time is and what day it is. I want it to show an image that says "open" between 7:25 - 12:40 and 13:30 - 14:10 when it's between the days monday to friday. On weekends and at any other time it should show the image "closed".

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

Displaying a different image depending on the time of the day, Displaying a different image depending on the time of the day and a little HTML , you can display different images according to the date/time. Notice that we used the "write" method to do so, with the html code enclosed in " " This is how we� Day of the month without leading zeros: 1 to 31: l (lowercase 'L') A full textual representation of the day of the week: Sunday through Saturday: N: ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday) S: English ordinal suffix for the day of the month, 2 characters: st, nd, rd or th.

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.

Displaying An Image On A Web Page Based Upon The Current , I'll start with Example 1, which shows a different image every hour, and because JavaScript's Date() function displays the hours of the day as� Thanks for all your help on this one. I succesfully got this working on the new site I'm creating. I needed to display a different image for each radio host at different points throughout the day, each day of the week. I also had to include a 2 hour offset from the server's time to match my time zone. Here's what my final code looks like.

Display different image each hour of the day (based on server time , View Full Version : Display different image each hour of the day info for date() is here-- http://www.php.net/manual/en/function.date.php but it� Formatting the Dates and Times with PHP. The format parameter of the date() function is in fact a string that can contain multiple characters allowing you to generate a date string containing various components of the date and time, like day of the week, AM or PM, etc.

Working with Dates and Times in PHP and MySQL, In this article I'll introduce you to the basics of PHP's time functions ( time() look at MySQL dates and show you how to make them play nicely with PHP. All other time zones in the world are expressed as a positive or negative offsets from this time. mktime(hour, minute, second, month, day, year, isDST). Displaying a different image depending on the time of the day. With the above knowledge, and a little HTML, you can display different images according to the date/time. Here is a common example: Displaying a different image depending on whether it is mourning or night. Lets first get two images: (One for mourning, one for night.)

PHP Date and Time, The PHP date() function is used to format a date and/or a time. d - Represents the day of the month (01 to 31); m - Represents a month (01 to 12); Y - Represents The example below formats today's date in three different ways: H - 24-hour format of an hour (00 to 23); h - 12-hour format of an hour with leading zeros (01� In this case, the difference is not 24 hour, so it will says 0 days. Better use "today", which should also use a default value like: Today: 00:00am, 01.01.2015 Tomorrow: 00:00am, 02.01.2015 which now is 24 hour and represents 1 day.

Comments
  • "I'm getting errors" 👈 what are the error, exactly?
  • I would almost guarantee that there is a more efficient way of coding this than a myriad of else ifs. Maybe try incorporating the date and time into the file name of each photo (for instance "02-15.jpg" would be seen on the 2nd day of the month after 3pm) and creating some logic based on that.
  • "The goal is to create if/else statements for all 31 possible days in a month" keep that in mind :)
  • Thanks @Barthy I have update my answer accordingly :)
  • Thank you @RohitMittal your help is much appreciated