DateTimeZone::__construct

(PHP 5 >= 5.2.0, PHP 7)
Creates new DateTimeZone object
DateTimeZone timezone_open ( string $timezone )

Object oriented style

public DateTimeZone::__construct ( string $timezone )

Procedural style

Creates new DateTimeZone object.

Parameters:
timezone

One of the supported timezone names.

Returns:

Returns DateTimeZone on success. Procedural style returns FALSE on failure.

Exception:

This method throws Exception if the timezone supplied is not recognised as a valid timezone.

Examples:
Catching errors when instantiating DateTimeZone
<?php
// Error handling by catching exceptions
$timezones = array('Europe/London', 'Mars/Phobos', 'Jupiter/Europa');

foreach ($timezones as $tz) {
    try {
        $mars = new DateTimeZone($tz);
    } catch(Exception $e) {
        echo $e->getMessage() . '<br />';
    }
}
?>

The above example will output:

DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Mars/Phobos)
DateTimeZone::__construct() [datetimezone.--construct]: Unknown or bad timezone (Jupiter/Europa)
doc_php
2016-02-24 15:56:24
Comments
Leave a Comment

Please login to continue.