W3cubDocs

/PHP

DateTimeZone::__construct

timezone_open

(PHP 5 >= 5.2.0, PHP 7)

DateTimeZone::__construct -- timezone_openCreates new DateTimeZone object

Description

Object oriented style

public DateTimeZone::__construct ( string $timezone )

Procedural style

timezone_open ( string $timezone ) : DateTimeZone|false

Creates new DateTimeZone object.

Parameters

timezone

One of the supported timezone names or an offset value (+0200).

Return Values

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

Errors/Exceptions

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

Changelog

Version Description
5.5.10 The timezone parameter accepts offset values.

Examples

Example #1 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)

© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/datetimezone.construct.php