tags:

views:

14

answers:

2

I need an exactly opposite method of timezone_name_from_abbr(): http://php.net/manual/en/function.timezone-name-from-abbr.php

I have a list of timezones name like:

Australia/Brisbane
Australia/Hobart
Asia/Vladivostok
Australia/Lord_Howe
Asia/Magadan
Asia/Kolkata
America/Los_Angeles
......

I need method which will take timezone as a param and return me a Abbreviation of it. For example:

If I use method like:

   $timeAbbr = timezone_abbr_from_name('America/Los_Angeles');
   echo $timeAbbr;

Result should be:

PST
A: 

DateTimeZone::ListAbbreviations will give you an associative array with all long names related to a specific abbreviation. It should be fairly straightforward to build a function with this that returns the correct abbreviation. There is nothing ready-made in the User Contributed Notes as far as I can see.

Pekka
Thnaks Pekka,Its a great solution though but was looking for a straight forward code , (method). Thanks anyways, I can use your suggestion in other part of my project. Thank You.
SachinKRaj
+3  A: 

This should work.

function timezone_abbr_from_name($timezone_name){
    $dateTime = new DateTime(); 
    $dateTime->setTimeZone(new DateTimeZone($timezone_name)); 
    return $dateTime->format('T'); 
}
GeekTantra
Thanks GeekTantaIt Works.
SachinKRaj
+1 sweet and easy!
Pekka