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

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
theJay
Forum Newbie
Posts: 2
Joined: Tue Nov 04, 2008 9:16 am

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
theJay
Forum Newbie
Posts: 2
Joined: Tue Nov 04, 2008 9:16 am

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

Post 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.
Post Reply