17

i've got users coming in from a different site and i'm getting that site to send across their timezone in a standard 'tz' format

 Antarctica/Casey    Antarctica/Davis   
 Antarctica/DumontDUrville  Antarctica/Macquarie 
 Antarctica/Mawson  Antarctica/McMurdo

How do i verify that this 'string' coming in is a VALID timezone entry?


this is what i'm doing

        $script_tz = date_default_timezone_get();
        if(!date_default_timezone_set($specifiedTimeZone))
        {
            date_default_timezone_set($script_tz);
            $errormessage = "Invalid TimeZone";
            return;         
        }
        date_default_timezone_set($script_tz);

but i dont like it - seems kludgy.


testing it out:

Test1

$test1 = 'America/New_York';
$test2 = 'junk';

$start = microtime(true);
for($i=1;$i<10000;$i++)
{
    if (in_array($test1, DateTimeZone::listIdentifiers())) {}else {}
    if (in_array($test2, DateTimeZone::listIdentifiers())) {}else {}
}

$end = microtime(true);

echo $end-$start;
?>

9.7208099365234

Test2

<?php

$test1 = 'America/New_York';
$test2 = 'junk';

error_reporting(0);
$start = microtime(true);
for($i=1;$i<10000;$i++)
{
        $script_tz = date_default_timezone_get();
        if(!date_default_timezone_set($test1))
        {
                date_default_timezone_set($script_tz);
        }
        else
                date_default_timezone_set($script_tz);
        $script_tz = date_default_timezone_get();
        if(!date_default_timezone_set($test2))
        {
                date_default_timezone_set($script_tz);
        }
        else
                date_default_timezone_set($script_tz);
}


$end = microtime(true);

echo $end-$start;
?>

0.25762510299683
siliconpi
  • 8,105
  • 18
  • 69
  • 107

5 Answers5

37

use DateTimeZone::listIdentifers()

if (in_array($timezone, DateTimeZone::listIdentifiers())) {
    echo "valid";
}
else {
    echo "invalid";
}
Jacob
  • 8,278
  • 1
  • 23
  • 29
  • is the date_default_timezone_set() a bad approach / incorrect approach? – siliconpi Apr 18 '11 at 16:38
  • i like the listIdentifiers approach better, but it seems like its much slower – siliconpi Apr 18 '11 at 17:09
  • @Frank D if you are going to do it many times then you should set DateTimeZone::listIdentifiers() to a variable... Running your tests again, using `$identifiers = DateTimeZone::listIdentifiers()` I get 0.31013107299805 and 0.16420483589172 – Jacob Apr 18 '11 at 23:39
  • @Frank D also if you are running it many times, I would flip the array. `$identifiers = array_flip(DateTimeZone::listIdentifiers())` and check `isset($indentifiers[$test1])`, that gets the time down to 0.0070869922637939 – Jacob Apr 18 '11 at 23:42
  • 1
    this is perfect. You link is dead. Here is an updated version - http://php.net/manual/en/datetimezone.listidentifiers.php – Eric Cope Feb 21 '13 at 05:21
  • Why DateTimeZone::listIdentifiers() does not return in its list "etc/UTC", but only "UTC", thus "etc/UTC" is considered invalid timezone? – Yasen Oct 09 '15 at 14:20
  • This list is flawed. It lacks some timezones that are otherwise accepted by `date_default_timezone_set`, like `America/Montreal` – njzk2 May 17 '17 at 21:05
0

Check out this: How to check is timezone identifier valid from code?

Report different approaches to solve your problem.

Community
  • 1
  • 1
eldblz
  • 758
  • 3
  • 11
  • 24
0

Validate against the tz database. There's http://code.google.com/p/tzdata/, that claims to provide the tz database in PHP format (whatever this means).

Oswald
  • 31,254
  • 3
  • 43
  • 68
  • how do i do that in php "properly" - using standard php functions and/or approaches? – siliconpi Apr 18 '11 at 13:12
  • To answer that question, I would have to download the database, extract it, examine it and write some sample code. I suggest you try this yourself and come back when you have problems with it. – Oswald Apr 18 '11 at 13:17
0

There is a helper: timezone_identifiers_list() will return an array of strings of timezones. then you can use something like in_array to validate it.

if (in_array($timezone, timezone_identifiers_list())) {
    // valid
}
Yamen Ashraf
  • 2,637
  • 2
  • 20
  • 26
-1

You could take the list of supported timezones, save it in a file and compare what you're getting to the list:

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

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99