Jump to content

Help! Change Picture at Time of Day with PHP


dawnrae

Recommended Posts

Hello, I've got a site I'm working on and I want a picture in the header to change according to the time of day, based on Central Standard Time. I've got a script that I found and I've been working with it, but it's not working correctly.

 

<?php 
$timeOffset = 0; // in hours, positive or negative
$nowHour = date("H", time() + (3600 * $timeOffset)) + 0;

if ($nowHour >= 7 && $nowHour < 9)
   $nowImg = 'austin-morning.jpg';
else if ($nowHour >= 9 && $nowHour < 19)
   $nowImg = 'austin-day.jpg';
else if ($nowHour >= 19 && $nowHour < 20)
   $nowImg = 'austin-evening.jpg';
else if ($nowHour >= 20 && $nowHour <=6)
   $nowImg = 'austin-night.jpg';
else $nowImg = 'austin-day.jpg';
?>

 

I'm pretty much PHP challenged except for incorporating scripts and making little edits here and there. Can anyone help me understand what's wrong here and why, even though my server time is CST, that the images are not showing up as they should?

Thank you in advance for any help anyone can offer.

 

Dawn

Hello, I've got a site I'm working on and I want a picture in the header to change according to the time of day, based on Central Standard Time. I've got a script that I found and I've been working with it, but it's not working correctly.

 

<?php 
$timeOffset = 0; // in hours, positive or negative
$nowHour = date("H", time() + (3600 * $timeOffset)) + 0;

if ($nowHour >= 7 && $nowHour < 9)
   $nowImg = 'austin-morning.jpg';
else if ($nowHour >= 9 && $nowHour < 19)
   $nowImg = 'austin-day.jpg';
else if ($nowHour >= 19 && $nowHour < 20)
   $nowImg = 'austin-evening.jpg';
else if ($nowHour >= 20 && $nowHour <=6)
   $nowImg = 'austin-night.jpg';
else $nowImg = 'austin-day.jpg';
?>

 

I'm pretty much PHP challenged except for incorporating scripts and making little edits here and there. Can anyone help me understand what's wrong here and why, even though my server time is CST, that the images are not showing up as they should?

Thank you in advance for any help anyone can offer.

 

Dawn

 

Dawn, my first thought is that you may not be actually displaying the image. This script is simply creating a variable to hold the source for your image tag. Make sure you're actually displaying the image elsewhere on your page:

<img src="/path/to/images/<?php echo $nowImg; ?>" />

 

Otherwise, as the others have said, it looks pretty good. Hope this helps!

Thank you for your replies. I think maybe your advice has helped. I made a couple changes, adding the || to the last else if statement:

 

 

<?php

$timeOffset = 1; // in hours, positive or negative
$nowHour = date("H", time() + (3600 * $timeOffset)) + 0;

if ($nowHour >= 7 && $nowHour < 9)
   $nowImg = 'austin-morning.jpg';
else if ($nowHour >= 9 && $nowHour < 19)
   $nowImg = 'austin-day.jpg';
else if ($nowHour >= 19 && $nowHour < 20)
   $nowImg = 'austin-evening.jpg';
else if ($nowHour >= 20 || $nowHour <=6)
   $nowImg = 'austin-night.jpg';
else $nowImg = 'austin-day.jpg';

?>

 

And also to the call within the page, where I'd initially had () around the $nowImg:

 

<?php echo $nowImg; ?>

 

And it seems to be working now. I'm going to keep track of it throughout the day to make sure.

Thank you all so much for helping out my php-challenged self. I truly appreciate it. If anyone else sees anything problematic in the code, feel free to chime in. I am grateful for all the advice I can get  :) .

I'll let you know how it goes!

 

Dawn

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.