tags:

views:

50

answers:

4

I was wonder what is the maximum length of the timezone settings in PHP? I'm storing the string in a database, and would like to keep the length as short as possible, but i see timezones like "Canada/East-Saskatchewan", which goes beyond our current limit.

If I could just get a list of all the supported timezone string, I can sort them, but they are currently split on to several different pages.

linky: http://www.php.net/manual/en/timezones.php

A: 

Every timezone can be abbreviated into 4 characters or less. (http://www.timeanddate.com/library/abbreviations/timezones/) So then maybe you can simple build a static array of the full name timezones linking them to their abbreviated name. This will allow you to use both.

$timeZone = 'Canada/East-Saskatchewan';
$tz = DateTimeZone::listAbbreviations();
echo recursiveArraySearch($tz, $timeZone);
function recursiveArraySearch($haystack, $needle, $index = null) {
    $aIt     = new RecursiveArrayIterator($haystack);
    $it    = new RecursiveIteratorIterator($aIt);
    while($it->valid()) {       
        if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
            return $aIt->key();
        }
        $it->next();
    }
    return false;
}
cdburgess
Not uniquely. For instance, notice that there are three different zones called "EST". Not only that, but the two kinds of descriptors don't even describe the same thing. For example, the Olson zone 'America/Denver` corresponds to the three-letter name 'MST' in winter and 'MDT' in summer, while 'America/Phoenix' corresponds to 'MST' year-round. At the very least you need a POSIX zone descriptor like 'MST7MDT,M3.2.0,M11.1.0' but even that actually contains less information :)
hobbs
Good catch. I forgot to account for daylight savings. I will look into working on figuring out how to accommodate for it.
cdburgess
+1  A: 

From the manual:

<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 5; $i++) {
    echo "$timezone_identifiers[$i]\n";
}
?>
Pekka
+4  A: 

Answer is 32. And here is the complete PHP code to find that:

<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();

$maxlen = 0;
foreach($timezone_identifiers as $id)
{
    if(strlen($id) > $maxlen)
        $maxlen = strlen($id);
}

echo "Max Length: $maxlen";

/*
Output:

Max Length: 32

*/
?>
shamittomar
It is important to note that the OP setting the width to 32 would be a huge mistake. It should be set higher - just in case.
Buggabill
I thought the answer is 42 - isn't it always 42?
Jonathan Leffler
@Jonathan - I was going to say this but did not want the OP to use it... :-)
Buggabill
I didn't know about the DateTimeZone class. Very helpful! Thanks teaching me how to fish. Also, I might just make the database width 42, I doubt anyone would check... Thanks again.
karlw
@karlw, in case you are not aware, the answer being 42 is making reference to the "answer to life, the universe, and everything" from the Hitchhikers Guide to the Galaxy. It was meant to be funny. ;)
cdburgess
+2  A: 

The Olson database - available from ftp://elsie.nci.nih.gov/pub/ (but see also http://www.twinsun.com/tz/tz-link.htm and http://en.wikipedia.org/wiki/Tz_database) - is the source of these names. The documentation in the file Theory includes a description of how the zone names are formed. This would help you establish how long names can be.

The longest 'current' names are 30 characters (America/Argentina/Buenos_Aires, America/Argentina/Rio_Gallegos, America/North_Dakota/New_Salem); the longest 'backwards compatibility' name is 32 characters (America/Argentina/ComodRivadavia).

Jonathan Leffler