PHP TZ setting length

2019-01-28 01:07发布

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

标签: php timezone
4条回答
唯我独甜
2楼-- · 2019-01-28 01:37

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)
查看更多
▲ chillily
3楼-- · 2019-01-28 01:46

From the manual:

<?php
$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i=0; $i < 5; $i++) {
    echo "$timezone_identifiers[$i]\n";
}
?>
查看更多
做自己的国王
4楼-- · 2019-01-28 01:49

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

*/
?>
查看更多
成全新的幸福
5楼-- · 2019-01-28 01:49

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).

查看更多
登录 后发表回答