Page 1 of 1

Help me convert/format UTC offset to hours "-270" = "-04:30"

Posted: Tue Nov 04, 2008 11:08 am
by theJay
Hopefully somebody can help me with this little bit of code, it's been awhile since I wrote php so I’m rusty and things have changed; I am coding this for PHP 5.2x to take advantage of the new date/time/dst functions.

Right now I have UTC timezone offsets in minutes "-660" through "840" stored in a database, and I want to display them as "-11:00" through "+14:00"; with leading zeros on single digit hours ā€œ-270ā€ would show "-04:30".

I want to do this with the least amount of code possible, using optimal functions; it took me too many lines of code and I know it can be done much better with less code.

Also, is there an existing database or function with the PHP timezones (http://us3.php.net/manual/en/timezones.php) to get the actual countries they belong to (php groups them in a very user unfriendly geographic way); there is almost 500 and I would hate to do the country codes manually; if you know any tricks to do this let me know and save me some major time.

To see what I am attempting to do with country/timezones drop-down list, login to google and go here: https://www.google.com/accounts/EditUserInfo (select country, uncheck display all countries).

Thanks for the help, hopefully down the road when I shake off the rust I can return the favor.

Re: Help me convert/format UTC offset to hours "-270" = "-04:30"

Posted: Tue Nov 04, 2008 1:49 pm
by requinix
For your first problem just do a bit of math.

Code: Select all

$offset = -270;
$hrmin = ($offset >= 0 ? "+" : "-"); $offset = abs($offset);
$hrmin .= sprintf("%02d:%02d", floor($offset / 60), $offset % 60);
For your second, I don't think PHP has much to help you. Look around on the web for a list that's already been done by someone else.

Re: Help me convert/format UTC offset to hours "-270" = "-04:30"

Posted: Tue Nov 04, 2008 2:18 pm
by theJay
tasairis wrote:For your first problem just do a bit of math.

Code: Select all

$offset = -270;
$hrmin = ($offset >= 0 ? "+" : "-"); $offset = abs($offset);
$hrmin .= sprintf("%02d:%02d", floor($offset / 60), $offset % 60);
For your second, I don't think PHP has much to help you. Look around on the web for a list that's already been done by someone else.
Perfect!

I'll keep looking for a database or way to match those timezones to countries and/or descriptions; worst case scenario I will have to enter them all manually, but should only take a day if it comes to that.

Thank you very much for the help.