6

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

Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
karlw
  • 668
  • 4
  • 13

4 Answers4

12

Edit June 2021 Answer is 64. Why? That's the width of the column used in MySQL to store those timezone name strings.

The zoneinfo database behind those time zone strings just added new prefixes. To America/Argentina/ComodRivadavia, formerly the longest timezone string, they added posix/America/Argentina/ComodRivadavia and right/America/Argentina/ComodRivadavia, both with a length of 38. This is up from 32, the previous longest string.

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

*/
?>
O. Jones
  • 103,626
  • 17
  • 118
  • 172
shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • 3
    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 Aug 13 '10 at 13:59
  • 11
    I thought the answer is 42 - isn't it always 42? – Jonathan Leffler Aug 13 '10 at 14:38
  • @Jonathan - I was going to say this but did not want the OP to use it... :-) – Buggabill Aug 13 '10 at 16:56
  • 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 Aug 14 '10 at 12:10
  • 3
    @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. ;) – Chuck Burgess Aug 16 '10 at 17:44
  • unless you fear they will spout out extra long timezone identifier, 42 is not that bad of an idea, giving around 10 char of leverage for new timezones... – Philippe Gilbert Jan 13 '15 at 21:48
  • This now comes up as 31, in 2016. – aalaap Aug 10 '16 at 05:50
  • 1
    I ran this snippet today and the max length was 30 with the longest value `America/Argentina/Buenos_Aires`. – user3601546 May 19 '20 at 06:03
9

The Olson database — available from ftp://ftp.iana.org/tz/releases/ or http://www.iana.org/time-zones (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).

* Note that the TwinSun site has not been updated for some time and has some outdated links (such as suggesting that the Olson database is available from ftp://ftp.elsie.nci.nih.gov — it is now available from IANA instead).

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

From the manual:

<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 5; $i++) {
    echo "$timezone_identifiers[$i]\n";
}
?>
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

If you are using MySQL with PHP, consider that MySQL already stores Olson timezone names in the "mysql" system database, in a table called "time_zone_name", in a column called "name". One option is to choose the length of your column to match the length that MySQL uses. In MySQL 5.7, the timezone name length is 64 characters. To determine the length in use on your MySQL installation, follow the example below:

mysql> use mysql
Database changed
mysql> describe time_zone_name;
+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| Name         | char(64)         | NO   | PRI | NULL    |       |
| Time_zone_id | int(10) unsigned | NO   |     | NULL    |       |
+--------------+------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
Michael Krebs
  • 1,212
  • 10
  • 11