time zone calculator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stella1229
Forum Newbie
Posts: 10
Joined: Thu Nov 14, 2013 1:24 pm

time zone calculator

Post by Stella1229 »

I would like get some ideals on how to create a PHP program that would take a date/time in a specific timezone (say Pacific Time Zone), and spit out the equivalent date/time in selected timezones around the world.

For example,
Jan 1 2014 08:00 in Pacific (PST)-> Jan 1 2014 4pm in GMT.
Mar 10 2014 08:00 in Pacific (now in PDT) -> Mar 10 2014 3pm in GMT
April 1 2014 08:00 in Pacific (PDT) -> April 1 2014 4pm (now in BST)

the program needs to be smart enough to do the conversion as well as figure out the current timezones around the world for that date/time.

Any ideas or guidance would be much much appreciated :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: time zone calculator

Post by requinix »

PHP has that all built-in. The entirety of your application is essentially

Code: Select all

date_default_timezone_set("America/Los_Angeles");
$time = strtotime("Jan 1 2014 08:00"); // interpreted according to America/Los_Angeles
date_default_timezone_set("GMT");
echo date("M j Y H:i", $time); // outputted according to GMT
There are alternate methods of course, like the DateTime class, and you'd have to get your time and timezones from some input somewhere, but that's the gist of it.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: time zone calculator

Post by Eric! »

Stella1229
Forum Newbie
Posts: 10
Joined: Thu Nov 14, 2013 1:24 pm

Re: time zone calculator

Post by Stella1229 »

Thanks for the information guys.. and please excuse my noob questions... How would I determine if UK is in GMT timezone or BST timezone for any given date/time the user enters?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: time zone calculator

Post by requinix »

For converting to? You'd then specify the timezone not as the actual timezone you're expecting but as the location you want to convert to.

For example, you might want to convert to "London" time (which covers much, but not all, of the UK).

Code: Select all

date_default_timezone_set("America/Los_Angeles");
$time = strtotime("Jan 1 2014 08:00");
date_default_timezone_set("Europe/London");
echo date("M j Y H:i (T)\n", $time);

date_default_timezone_set("America/Los_Angeles");
$time = strtotime("Mar 10 2014 08:00");
date_default_timezone_set("Europe/London");
echo date("M j Y H:i (T)\n", $time);

date_default_timezone_set("America/Los_Angeles");
$time = strtotime("April 1 2014 08:00");
date_default_timezone_set("Europe/London");
echo date("M j Y H:i (T)\n", $time);

Code: Select all

Jan 1 2014 16:00 (GMT)
Mar 10 2014 15:00 (GMT)
Apr 1 2014 16:00 (BST)
Stella1229
Forum Newbie
Posts: 10
Joined: Thu Nov 14, 2013 1:24 pm

Re: time zone calculator

Post by Stella1229 »

Bow to your greatness! Tremendous help!
Post Reply