Jump to content

Recommended Posts

Hey all,

I'm putting together a script for an on-air-now section of a radio station website. The info for each DJ's timeslot is being pulled from a database. My problem is finding the right code to come up with the "local"(the user's) day of the week and time of day and compare that to the same info relative to the radio stations region. There are plenty of Javascripts that perform this exact function, but finding the corresponding PHP functions that would do the same thing has been illusive. I've done plenty of reading on php.net about localtime(), getdate() and date_default_timezone_set(), but how to juggle the variables using any of these functions to get the desired result is anything but apparent in the examples on php.net or any other online source that talks them.

 

Does anyone have an idea as to how I would establish a default timezone with all its' relative info and use it to do the proper time/day conversion for a user's localtime?

 

Mark

Link to comment
https://forums.phpfreaks.com/topic/51508-trying-to-make-an-on-air-now-feature/
Share on other sites

Yes, I suppose setting a javascript cookie for both pieces of info before the php code could work. The question is, can all browsers set a cookie fast enough for it to get plugged into a php script on the same page? Knowing how differently IE and Firefox can handle cookies, I would tend to be suspicious of that solution.

Why would you care what the time is on the user's computer? As long as you know what the time is on the server and you know what time each DJ is on the air, that is all you need to know. So, if the server is on Pacific Time, then you would probably want to have an entry for each DJ for the local time of their show as well as have an offset to pacific time - or alternatively use GMT time as the standard. Then based upon the server's time or GMT time you could see if the DJ is on-air.

All that you've suggested currently makes up my "Plan B" which, for the sake of time, I'll probably end up doing. However, "Plan A" is to convert the DJ's timeslot to the user's time for people who may want an easy way of immediately knowing what time they can hear a certain DJ on an East Coast radio station if the user is on the West Coast. That's why I was wanting to convert the day/time variables...

 

I hear a clock ticking...

 

I think I'll implement Plan B for now...

 

Thanks, MadTechie!

Well, I think your title could have been better. I was under the impression you wanted an "On Air Now" feature. In which case you only need to know when the DJ is "on air".

 

In any event, you still do not need to know the user's local time in order to implement what you want. All you need to know is what the server's current time is and the time that the show will be on - in the server's local time (or you can use GMT for both). Then you can use client-side javascript to display the show's time in the user's local time.

 

So, let's say the server is in Central time and it is currently 12:00 noon. Then let's say there is a radio show on the west coast that starts at 2PM Pacific. So, that show would have a Central start time of 4PM. The difference is four hours. When creating the page for the user you could add the following code 1) Display the time zone specific time in a div or other control, and 2) Changing the +4 in the 2nd line of javascript to the appropriate value based upon the calculations above.:

 

<div id="show_time">2:00PM PST</div>

<script type="text/javascript">
  showtime = new Date();
  showtime.setHours(showtime.getHours()+4);
  showtime = showtime.toTimeString();
  document.getElementById('show_time').innerHTML = showtime;
</scrpt>

 

So, if the user has Javascript disabled they will still see the correct show time, but it will be in the shows local time zone. If Javascript is enabled it will show the time in the user's current time zone.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.