Login to participate
  
       
Register   Lost ID/password?
Louis Kessler’s Behold Blog » Blog Entry           prev Prev   Next next

PHP Timezones - Wed, 30 Jul 2008

I’ve had to figure out 100 different puzzles to get the Blog and Forum going. An example is figuring out how PHP determines the time in different timezones.

It bothered me to see that both WordPress and bbPress display times only in terms of either Greenwich Mean Time or in the local time where the Server is located. In my case, my server in in North American Eastern Time, but I am located in Central Time. All the times displayed appeared to me to be one hour off my local time.

I thought PHP should be able to detect the timezone of the user, but after some research I realized it can’t. The reason is it has no access to the user’s timezone, since the user’s local time is not a browser variable that is sent to the webserver. Javascript can get it, but I really didn’t want to add any Javascript to my pages unless it was absolutely necessary.

What many Forums do is to allow a person to enter their timezone in their profile. That would work, but the way I’ve set things up, there is currently no profile information in my Blog/Forum to set up after you’ve registered. It would require another step, and a simple one at that, but it’s something that just doesn’t feel necessary to me if it can be done automatically.

An automatic example, and something I did include, is to use the visitor’s IP address, which is passed to the server and available to PHP, to look up the country and a little country flag. The database for this is available free from ip2nation and there is a nice plugin for WordPress available to make it easy to do this.

So if there were an ip-to-timezone database to do the same, it would be easy. But unfortunately that beast is only available commercially and it doesn’t come cheap. We’re talking $649 for their least expensive database that includes timezone.

One feasible option would be when a person registers, and/or when they login, to use the JavaScript function to save their timezone with their user information.

The other option is not to use the timezone at all. What bbPress did was quite interesting. Instead of listing the Date and Time as most forums do, they list how old the post is, e.g. 3 months old or 6 days old or 25 seconds old. In some ways that is very nice and will always work no matter what timezone the visitor is in. However, seeing “1 year old” doesn’t differentiate a post that might be anywhere from 1 year to 1 year and 364 years ago. Also relating those long ago periods into a timeframe context (i.e. when was that?) is not easy for most people, myself included.

WordPress and bbPress don’t use the same style. WordPress lists dates. bbPress lists time-ago. That’s not a difference I wanted in my integrated site.

So in the end, I didn’t decide to use Javascript, nor have a user option, nor use the IP address. My compromise is to list the “how long ago” up to 7 days ago, and when longer than that to show the date. This will be common to both my Blog and Forum. (I will ignore the minor problem that the date may be one day off because of the timezone difference, which was always a problem anyway.)

So this is an example of one of the interesting puzzles that have been taking my time. This one was about 8 hours of work over 4 days. But it was fun and I learned a lot about all sorts of web technologies.

———————

Hopefully the web-puzzles are just about over, and I’ll be back to Behold again. I know I’ve been saying this for the last six months, and Behold has been delayed as a result, but bear with me just a bit longer. My old Behold site is 8 years old and needed this rejuvination. The new site (beholdgenealogy.com) that I am finishing will have new technology that should be good hopefully for another 8 years. I’ll soon be able to concentrate on Behold with less interruptions than I would have otherwise. Thanks for your patience.

2 Comments           comments Leave a Comment

1. richardxi (richardxi)
Great Britain (UK) flag
Joined: Mon, 25 Aug 2008
1 blog comment, 0 forum posts
Posted: Mon, 25 Aug 2008  Permalink

You can also use the World Time Engine IP-to-Local Time API. I believe they have a free trial system where you can try their API and if it’s central to what you’re doing the API is really good.

2. Louis Kessler (lkessler)
Canada flag
Joined: Sun, 9 Mar 2003
135 blog comments, 199 forum posts
Posted: Wed, 27 Aug 2008  Permalink

That’s an interesting looking system that I had overlooked. It’s much cheaper than IP2location, but it still is not free.

If getting the timezone or location was a service I really needed for my product, then I would definitely consider World Time Engine. But for the purposes of adding one extra little feature to my blog and forum, it’s not worth the cost and effort to me.

But I see there are some WordPress plugins that use it. The one called “I am Here” looks really neat!

 

The Following 1 Site Has Linked Here

  1. Recent Links Tagged With "timezones" - JabberTags : Tue, 26 Aug 2008
    [...] public links >> timezones PHP Timezones Saved by ralfshaftoe on Mon 25-8-2008 Toddlers Across the Time Zones Saved by kakashi2555 on Mon [...]

Leave a Comment

You must login to comment.

Login to participate
  
       
Register   Lost ID/password?