Sun 20 May 2007
The Setup
At my job one of the products we offer is a radio service. There are a bunch of channels and each channel has a bunch of playlists. I was tasked with writing something to automatically switch the active playlist for each channel. My initial thought was to make a flag in the database marking the active playlist and then write a script that would be run from cron.
The Solution
After a bit of messing around I came up with something better. It didn’t need the database or cron and it was one line:
$active = round(time() / PLAYLIST_ROTATE_PERIOD) % count($playlists);
Why It Works
Dividing the current unixtime by the amount of time you want each playlist to be active for leads to a grouping of time chunks. Say you want each playlist to run for 3 hours (10800 seconds): round(1179685800 / 10800) = 109230.
Now 15 minutes later: round(1179686700 / 10800) = 109230. Same result.
Now 3 hours later: round(1179696600 / 10800) = 102931. The result increases one, which when modded by the number of playlists gives you the id of the next one in the list.
Popularity: 8% [?]










