Hello,

I need to rotate images in a website based on the time of day. Please help out with this code.

regards,
michael

Recommended Answers

All 11 Replies

You can rotate image in many ways
in random on each page refresh
in sequence on each refresh
Rotate with respect to time

Which type you want?

You can rotate image in many ways
in random on each page refresh
in sequence on each refresh
Rotate with respect to time

Which type you want?

hello,

Thanks for the reply, i'd like to rotate with respect to time

Hello to get the time use the date() format:

//get hour in 24 hour format
$hour = date("H");

if($hour >= 06 && $hour < 12){
 echo "<img src='morningimage.jpg' />";
}elseif($hour >= 12 && $hour < 18){
 echo "<img src='afternoonimage.jpg' />";
}

Simply repeat for whatever time you want.

the code looks good, is it possible to include the day of the week in this code? such that you can also customize images for saturdays and sundays? thanks

Hello to get the time use the date() format:

//get hour in 24 hour format
$hour = date("H");

if($hour >= 06 && $hour < 12){
 echo "<img src='morningimage.jpg' />";
}elseif($hour >= 12 && $hour < 18){
 echo "<img src='afternoonimage.jpg' />";
}

Simply repeat for whatever time you want.

Thanks man. would it be possible to add some code to control particular images for specific days of the week?

Same idea just check http://php.net/manual/en/function.date.php

Change the parameters (hour to day) and the if statements to reflect the Days as output in php.

I urge you to try this yourself so you can get used to using php.

If you get stuck then post your code etc.

<?php
      date_default_timezone_get
   
      $hour = date("gettimeofday(true)");
  
      if($hour >= Sun 06 && $hour < 12){

      echo "<img src='sun_morningimage.jpg' />";
  
      }elseif($hour >= Sun 12 && $hour < 18){
  
      echo "<img src='sun_afternoonimage.jpg' />";}

      if($hour >= Mon 06 && $hour < 12){

      echo "<img src='mon_morningimage.jpg' />";
  
      }elseif($hour >= Mon 12 && $hour < 18){
  
      echo "<img src='mon_afternoonimage.jpg' />";}

      if($hour >= Tue 06 && $hour < 12){

      echo "<img src='tue_morningimage.jpg' />";
  
      }elseif($hour >= Tue 12 && $hour < 18){
  
      echo "<img src='tue_afternoonimage.jpg' />";}

      if($hour >= Wed 06 && $hour < 12){

      echo "<img src='wed_morningimage.jpg' />";
  
      }elseif($hour >= Wed 12 && $hour < 18){
  
      echo "<img src='wed_afternoonimage.jpg' />";}
 
      if($hour >= Thu 06 && $hour < 12){

      echo "<img src='thu_morningimage.jpg' />";
  
      }elseif($hour >= Thu 12 && $hour < 18){
  
      echo "<img src='thu_afternoonimage.jpg' />";}

      if($hour >= Fri 06 && $hour < 12){

      echo "<img src='fri_morningimage.jpg' />";
  
      }elseif($hour >= Fri 12 && $hour < 18){
  
      echo "<img src='fri_afternoonimage.jpg' />";}
 
     if($hour >= Sat 06 && $hour < 12){

      echo "<img src='sat_morningimage.jpg' />";
  
      }elseif($hour >= Sat 12 && $hour < 18){
  
      echo "<img src='sat_afternoonimage.jpg' />";}
?>

Same idea just check http://php.net/manual/en/function.date.php

Change the parameters (hour to day) and the if statements to reflect the Days as output in php.

I urge you to try this yourself so you can get used to using php.

If you get stuck then post your code etc.

Hello,

This is the edited code for website image rotation. Thanks

<?php
      
      date_default_timezone_get
   
      $hour = date("gettimeofday(true)");
  
      if($hour >= Sun 06 && $hour < 12){

      echo "<img src='sun_morningimage.jpg' />";
  
      }elseif($hour >= Sun 12 && $hour < 18){
  
      echo "<img src='sun_afternoonimage.jpg' />";}

      if($hour >= Mon 06 && $hour < 12){

      echo "<img src='mon_morningimage.jpg' />";
  
      }elseif($hour >= Mon 12 && $hour < 18){
  
      echo "<img src='mon_afternoonimage.jpg' />";}

      if($hour >= Tue 06 && $hour < 12){

      echo "<img src='tue_morningimage.jpg' />";
  
      }elseif($hour >= Tue 12 && $hour < 18){
  
      echo "<img src='tue_afternoonimage.jpg' />";}

      if($hour >= Wed 06 && $hour < 12){

      echo "<img src='wed_morningimage.jpg' />";
  
      }elseif($hour >= Wed 12 && $hour < 18){
  
      echo "<img src='wed_afternoonimage.jpg' />";}
 
      if($hour >= Thu 06 && $hour < 12){

      echo "<img src='thu_morningimage.jpg' />";
  
      }elseif($hour >= Thu 12 && $hour < 18){
  
      echo "<img src='thu_afternoonimage.jpg' />";}

      if($hour >= Fri 06 && $hour < 12){

      echo "<img src='fri_morningimage.jpg' />";
  
      }elseif($hour >= Fri 12 && $hour < 18){
  
      echo "<img src='fri_afternoonimage.jpg' />";}
 
     if($hour >= Sat 06 && $hour < 12){

      echo "<img src='sat_morningimage.jpg' />";
  
      }elseif($hour >= Sat 12 && $hour < 18){
  
      echo "<img src='sat_afternoonimage.jpg' />";}

?>

Hello i am new to PHP scripting and can i know how to use the script? please.
I think that i should add the location of the script to an " <img src= #script location.php> " . am i correct ??:?:

Hello i am new to PHP scripting and can i know how to use the script? please.
I think that i should add the location of the script to an " <img src= #script location.php> " . am i correct ??:?:

No. You might want to start a new topic for questions like this.

PHP scripts are characterized typically by the file extension .php. The server software needs to know how to find the PHP script engine (this is configured in the web server configuration) and will then automatically execute everything which it thinks is PHP.

Example 1: a file index.html

<body>
Some text
<?php 
  echo "Here we would expect some output from PHP";
?>
Some more text
</body>

This will NOT work. The server thinks it is pure HTML because of the extension .html and will not process it.

Example 2: a file something.php

<body>
Some text
<?php 
  echo "Here we would expect some output from PHP";
?>
Some more text
</body>

This will work if PHP is correctly installed inside the web server. Processing will start in line 3. Then the echo from line 4 will be processed. In line 5 processing stops. All the rest (outside the PHP tag) will be taken as is and will not get processed.

I think it would be more efficient to find the information seperately then combine them at the end. If your images are named in this format (mon_morningimage.jpg) it makes it very easy.

//get day in 3 letter format i.e. Mon,Tue etc
$getDay = date("D");
//convert the day to lowercase i.e. Mon -> mon
$getDay = strtolower($getDay);
//get the hour of the day in 2 digits i.e. 6am returns 06 and 6pm returns 18
$getHour = date("H");

//determine the time of day i.e. morning/afternoon/evening
if($getHour > 12){
 $dayPortion = "afternoon";
}elseif($getHour > 17){
 $dayPortion = "evening";
}elseif($getHour > 21){
 $dayPortion = "night";
}elseif($getHour > 03 && $getHour <= 12){
 $dayPortion = "morning";
}

//createimage string dependant on what we gathered i.e. mon_afternoonimage.jpg
echo "<img src='".$getDay."_".$dayPortion."image.jpg' alt='".$getDay." ".$dayPortion." Image' />";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.