I spent about 15 minutes writing a small PHP function for one of my sites, and then another hour writing it up. What for?

Well, let’s back up. If you are running online events where people might come from anywhere, there can always be challenges for getting the right time to people. For our NMC events, while our office is in Austin, we have staff in 3 other time zones, and people in our community from maybe another 14 or more zones on top of that.

If I say something starts at 2:00pm Central Time, then it puts it on everyone else to figure out when that is (and I’ve seen event sites where they do not even provide the reference time zone). If were all proper and versatile, we’d use Greenwich Mean Time, since everyone knows their offset, right? (I am -7 what are you?)

Most of the time , our event times are referenced to Pacific Time, since that is where our server is hosted, but even if we provide Pacific, Central, and Eastern Time, what about folks in Spain? New Zealand? Hong Kong?

This came to me a lot when our events picked up in Second Life back in 2006-2007. They used Pacific Time too as a reference time (actually for a long time they called it SLT for Second Life Time, and people still say that).

I started early using the World Time Clock fixed time. It allows you to enter a date time, and reference time zone, and generates a link that produces a big chart that converts that to other times around the world.

it’s silly to not think of the world as your audience, and most of the world is on a different time zone, like those weird half hour ones in South Australia.

For example, if there was an event, like tonight my dinner, at 6:00 PM, I’d enter the date and time in the form, and scroll all the way down to the bottom to select my time zone (U.S.A. – Arizona – Phoenix), and the form generates for me this URL

http://www.timeanddate.com/worldclock/fixedtime.html?month=10&day=23&year=2010&hour=18&min=0&sec=0&p1=197

which I can use to generate a URL that lets anyone else in the world know when I am having dinner, in their local time zone:

And in our event web pages I might say:

hey, tonight’s event is Alan’s Dinner! It is taking place October 22 at 6:00 PM PST (check for local time)

I was pretty adamant about getting our folks to add this “check for local time” link. If you inspect the URL, you can actually make new ones just by adjusting the values in the URL string (The site does provide a URL shortener if you dont want to display a monster long URL).

But here is the thing that struck me today- for our MIDEA site, I am using the WP custom post types for our events, so I already have built into the date, a value for the start date/time (it is the unix time version of the date/time, number of seconds since 1970). Since I already have the data, I thought it would be easy to write a function that auto generates the World Time Clock Link.

It was easy.

function get_worldtimeclock ($ts, $tz=197) {
	// generates a URL for a world time clock link for a given timestamp $ts
	// default timezine (for this site) is 197 or Phoenix, hey thats my time zone
		
	$when = getdate($ts);
		
	return 'http://www.timeanddate.com/worldclock/fixedtime.html?month=' . $when['mon']  . '&day=' . $when['mday']  . '&year=' . $when['year']  . '&hour=' . $when['hours']  . '&min=' . $when['minutes']  . '&sec=0&p1=' . $tz;
}

and you can see it in action in the top gray bars on pages like http://midea.nmc.org/event/horizonreportmuseumwebinar/.

It’s really bone simple PHP, just like to write it up. I set up a code demo page so you can see it in action:

Actually, it was just fun to dip my paws into the code. Even if it is bone head simple code.

The post "PHP Local Time Code" was originally scraped from the bottom of the pickel barrel at CogDogBlog (http://cogdogblog.com/2010/10/php-local-time-code/) on October 22, 2010.

2 Comments

Leave a Comment

All fields are required. Your email address will not be published.