(PHP 5 >= 5.2.0, PHP 7)
DateTimeZone::__construct -- timezone_open — Creates new DateTimeZone object
Object oriented style
$timezone
)Procedural style
Creates new DateTimeZone object.
Returns DateTimeZone on success.
Procedural style returns FALSE
on failure.
This method throws Exception if the timezone supplied is not recognised as a valid timezone.
Version | Description |
---|---|
5.5.10 |
The timezone parameter accepts offset values.
|
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)