Title: Timed Images Code
Last modified: August 19, 2016

---

# Timed Images Code

 *  [citizenkeith](https://wordpress.org/support/users/citizenkeith/)
 * (@citizenkeith)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/)
 * I’m using WordPress as a CMS for a radio station. I’m trying to display an image
   during certain hours of the day that corresponds to the current show that is 
   on the air.
 * I found some discussion here:
    [http://ask.metafilter.com/50423/Rotating-header-based-on-time-of-day](http://ask.metafilter.com/50423/Rotating-header-based-on-time-of-day)
 * One uses javascript, the other uses PHP. I decided to go with the PHP solution.
 * I’m not an expert coder, so I have no idea why it’s not working. 😀
 * Here’s the schedule
 * 1am-6am Show 1
    6am-9am Show 2 9am-12pm Show 3 12pm-3pm Show 4 3pm-6pm Show 5
   6pm-8pm Show 6 8pm-10pm Show 7 10pm-1am Show 8
 * Here’s the code:
 *     ```
       <img src="http://www.domain.com/images/rotation/picture<?php
       $hour = date('H'); // hour of the day, 24 hour clock
       if ($hour > 1 or $hour < 6) {
       	$timepic = '0100pic';
       } elseif ($hour > 6 or $hour < 9) {
       	$timepic = '0600pic';
       } elseif ($hour > 9 or $hour < 12) {
       	$timepic = '0900pic';
       } elseif ($hour > 12 or $hour < 15) {
       	$timepic = '1200pic
       } elseif ($hour > 15 or $hour < 18) {
       	$timepic = '1500pic';
       } elseif ($hour > 18 or $hour < 20) {
       	$timepic = '1800pic';
       } elseif ($hour > 20 or $hour < 23) {
       	$timepic = '2000pic';
       } elseif ($hour > 23 or $hour < 1) {
       	$timepic = '2300pic';
       } else {
       	$timepic = 'nobody';
       }
       echo $timepic;
       ?>.jpg" alt="On The Air Now" width="340" height="200" />
       ```
   
 * The image doesn’t change from the first image, picture0100pic.jpg.
 * Any ideas? Also, how would I display these only during weekdays, and using picturenobody.
   jpg during the weekend?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676659)
 * Instead of
    `$hour = date('H'); // hour of the day, 24 hour clock`
 * Try using this:
 *     ```
       $now = getdate();
       $hour = $now['hours'];
       ```
   
 * While it shouldn’t make any difference, I can see where it might. PHP is a typed
   language, it just hides the types well.
 * As for how you display weekends, well, that’s simpler.
 *     ```
       $weekday = $now['wday']; // 0 = Sun, 6 = Sat
       if ($weekday == 0 or $weekday == 6) {
         $timepic = 'weekend';
       } elseif ($hour > 1 or $hour < 6) {
       	$timepic = '0100pic';
       ... etc ...
       ```
   
 * As for why it doesn’t change… Are you using WP-Cache or WP-Super-Cache? Those
   will cache the whole page, including the results of running this code. If you’re
   using either of those, you may want to set the expire time to 20-30 minutes or
   so.
 *  Thread Starter [citizenkeith](https://wordpress.org/support/users/citizenkeith/)
 * (@citizenkeith)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676685)
 * Thanks Otto,
 * I made the recommended change, and that didn’t do anything. And since I’m not
   using WP-Cache or WP-Super-Cache, I was at a loss.
 * However, I made this change:
    `($hour > 1 or $hour < 6)`
 * to
 * `($hour > 1 and $hour < 6)`
 * That worked. 🙂
 * If I knew anything about making plugins, I’d see if I could adapt this script.
   But I still have a LOT of work to do on this site, so I probably won’t be learning
   how to write plugins anytime soon. If anybody’s interested in tackling this one,
   go for it. 🙂
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676690)
 * Damn. I can’t believe I missed that logic error. Probably because you used the
   word `or` instead of the more common `||` symbol. If it had been `||`‘s I would
   have spotted it instantly. 🙂
 *  Thread Starter [citizenkeith](https://wordpress.org/support/users/citizenkeith/)
 * (@citizenkeith)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676698)
 * Like I said, I’m no expert coder. I’m just guessing for the most part. 🙂
 * However, it’s 3pm and the banner just defaulted to picturenobody.jpg. Weird.
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676700)
 * No, now that logic error I do see.
 *     ```
       ...
       } elseif ($hour > 12 or $hour < 15) {
       	$timepic = '1200pic
       } elseif ($hour > 15 or $hour < 18) {
       	$timepic = '1500pic';
       ```
   
 * What happens when the hour is 3 pm exactly? Ahh, that one is not covered.
 * You need to make all of them like this:
 *     ```
       } elseif ($hour >= 12 && $hour < 15) {
       	$timepic = '1200pic
       } elseif ($hour >= 15 && $hour < 18) {
       	$timepic = '1500pic';
       ```
   
 * The >= is a greater-than-or-equal-to.
 *  Thread Starter [citizenkeith](https://wordpress.org/support/users/citizenkeith/)
 * (@citizenkeith)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676707)
 * Thanks! That did the trick.
 * Now I’ll try out the weekend code and see what happens. But I’ll have to wait
   until the weekend, I guess. 😀
 *  Thread Starter [citizenkeith](https://wordpress.org/support/users/citizenkeith/)
 * (@citizenkeith)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676841)
 * How does this look? One show is on from 1am-4am, Tuesday through Saturday.
 *     ```
       <img src="http://www.domain.com/picture<?php
       $now = getdate();
       $hour = $now['hours'];
       $weekday = $now['wday']; // 0 = Sun, 6 = Sat
       if ($hour >= 1 && $hour < 4 && weekday >=2) {
       	$timepic = '0100pic';
       } elseif ($hour >= 6 && $hour < 9) {
       	$timepic = '0600pic';
       } elseif ($hour >= 9 && $hour < 12) {
       	$timepic = '0900pic';
       } elseif ($hour >= 12 && $hour < 15) {
       	$timepic = '1200pic';
       } elseif ($hour >= 15 && $hour < 17) {
       	$timepic = '1500pic';
       } elseif ($hour >= 17 && $hour < 18) {
       	$timepic = '1700pic';
       } elseif ($hour >= 18 && $hour < 20) {
       	$timepic = '1800pic';
       } elseif ($hour >= 20 && $hour < 23) {
       	$timepic = '2000pic';
       } elseif ($hour >= 23 || $hour < 16) {
       	$timepic = '2300pic';
       } else {
       	$timepic = 'nobody';
       }
       echo $timepic;
       ?>.jpg" alt="On The Air Now" />
       ```
   
 *  Thread Starter [citizenkeith](https://wordpress.org/support/users/citizenkeith/)
 * (@citizenkeith)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676844)
 * Sorry, posted the wrong code. And it’s not working. 🙁
 *     ```
       <img src="http://www.domain.com/picture<?php
       $now = getdate();
       $hour = $now['hours'];
       $weekday = $now['wday']; // 0 = Sun, 6 = Sat
       if ($hour >= 1 && $hour < 4 && $weekday >= 2) {
       	$timepic = '0100pic';
       } elseif ($hour >= 6 && $hour < 9 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '0600pic';
       } elseif ($hour >= 9 && $hour < 12 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '0900pic';
       } elseif ($hour >= 12 && $hour < 15 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '1200pic';
       } elseif ($hour >= 15 && $hour < 17 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '1500pic';
       } elseif ($hour >= 17 && $hour < 18 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '1700pic';
       } elseif ($hour >= 18 && $hour < 20 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '1800pic';
       } elseif ($hour >= 20 && $hour < 23 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '2000pic';
       } elseif ($hour >= 23 || $hour < 16 && $weekday >= 1 && $weekday <= 5) {
       	$timepic = '2300pic';
       } else {
       	$timepic = 'nobody';
       }
       echo $timepic;
       ?>.jpg" alt="On The Air Now" />
       ```
   
 * I want every image to appear Monday through Friday only, except 0100pic which
   will appear Tuesday through Saturday. All other times the default “nobody” image
   will appear.
 * The time of day is working correctly, but the day of the week isn’t working. 
   It’s Saturday, and all the regular images are showing.
 *  [Republic](https://wordpress.org/support/users/republic/)
 * (@republic)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676868)
 * citizenkeith – were you able to get the code to work? I have almost the same 
   need for the code for a similar application.
 * If it is working, can you post the code and a link to your site?
 * Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Timed Images Code’ is closed to new replies.

## Tags

 * [images](https://wordpress.org/support/topic-tag/images/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [rotation](https://wordpress.org/support/topic-tag/rotation/)
 * [timed](https://wordpress.org/support/topic-tag/timed/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 9 replies
 * 3 participants
 * Last reply from: [Republic](https://wordpress.org/support/users/republic/)
 * Last activity: [18 years, 4 months ago](https://wordpress.org/support/topic/timed-images-code/#post-676868)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
