Making massive comparisons with PHP
September 16, 2007 7:13 AM   Subscribe

PHP gurus - what's the most elegant way to display a variable bit of text on a website sidebar based on time-of-day and day-of-week?

Building a page that will show, in real time, what program is currently playing on a radio station. So on Sunday mornings between 7 and 8, it needs to display one string of text; from 8 to 10, something else, and so on.

I know I can do this with a massive series of if/then/else comparisons, but is there a better way? My reluctance to this approach comes from knowing that schedules change, and modifying this logic would be a pain.
posted by jbickers to Computers & Internet (8 answers total)
 
When faced with something like this, I usually either put it in a database or use a giant Switch statement...
posted by SpecialK at 7:27 AM on September 16, 2007


Well, this is sort of open ended, but syntactically, you get slightly more elegant, or at least concise code with the switch statement or the ternary operator.

But I think what you are asking is how you should deal with the logic. There are probably going to be a lot of answers, but the best one is going to match your specific criteria. And you've only given us a bit of that.
posted by miniape at 7:31 AM on September 16, 2007


This is probably not the most efficient way, but you could use include() to pull a series of cleverly named html segments from a directory.. i.e. include($day-$time.html) if you had a file named sunday-1012.html.
posted by softlord at 8:07 AM on September 16, 2007


You could set up a 2-dimensional array as a way of giving it structure and flexibility without resorting to a database.
posted by malevolent at 8:21 AM on September 16, 2007


A simple database table with program name, start time, end time, and day of week should do it. Then just get the current server time, offset it if hosted in another time zone, and query the database for the day of week and greater than start time and less than end time. Without a database, you could do something with just a flat file loaded into an array and iterated until you find the corresponding entry.
posted by hungrysquirrels at 8:35 AM on September 16, 2007


Use an array. Array lookups are very fast; much faster than connecting to a database. Write a function that takes a date and time, truncates and modulos it so that each instant between 7am and 8am on Sunday gives the same value. Use that value as an index into the array.
posted by ijoshua at 8:41 AM on September 16, 2007


I'd probably use a database; it's likely to be more flexible and easier to maintain than either a huge array or lots of spaghetti logic. Something like:

Schedule(id, name, description, dow, starts_at, ends_at)

Then SELECT * FROM Schedule WHERE dow = WEEKDAY(NOW()) AND starts_at >= CURTIME() AND ends_at < CURTIME().

With an index on (dow,starts_at,ends_at) this should be nice and fast, and easier to make a frontend for making and verifying changes.

Note you might want to break out another table out of this for programs, so the same one on at different times/days share the same text.
posted by Freaky at 10:59 AM on September 16, 2007


Best answer: To build on ljoshua's suggestion: make a simple array of small associative arrays. This is basically the same thing as having a database, except you don't need to have a database.

Each associative array would have three keys: "name", "start", and "stop". Assuming that your schedule repeats every week, have the "start" and "stop" values look like "Sunday 09:00" or "Saturday 15:00" - i.e., the weekday name plus 24-hour time from "00:00" to "24:00". "Name" is just the show name you want to display. Have your script iterate over the main array and for each item, check whether the current day/time is between "start" and "stop" - if yes, then print out "name" and bail out.

Gotchas: shows that cross midnight (enter these twice, one stopping at 24:00 and the other starting at 00:00), time zones (make sure your server knows what time zone the radio station is in).

Here's a minimalist example: Making-massive-comparisons-with-PHP.phps.
posted by migurski at 11:16 AM on September 16, 2007 [1 favorite]


« Older 150 Channells and Nothing To Watch   |   How do I become a business tycoon? Newer »
This thread is closed to new comments.