DateTimeZone::__construct

timezone_open

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

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 a new DateTimeZone object.

A DateTimeZone object provides access to three different types of timezone rules: UTC offset (type 1), timezone abbreviation (type 2), and timezone identifiers as published in the IANA timezone database (type 3).

The DateTimeZone object can be attached to DateTime and DateTimeImmutable objects to be able to render the timezone encapsulated by these objects in a local timezone.

Parameters

timezone

One of the supported timezone names, an offset value (+0200), or a timezone abbreviation (BST).

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.

Examples

Example #1 Creating and attaching DateTimeZone to a DateTimeImmutable

<?php
$d 
= new DateTimeImmutable("2022-06-02 15:44:48 UTC");

$timezones = [ 'Europe/London''GMT+04:45''-06:00''CEST' ];

foreach (
$timezones as $tz) {
    
$tzo = new DateTimeZone($tz);

    
$local $d->setTimezone($tzo);
    echo 
$local->format(DateTimeInterface::RFC2822 ' — e'), "\n";
}
?>

The above example will output:


Thu, 02 Jun 2022 16:44:48 +0100 — Europe/London
Thu, 02 Jun 2022 20:29:48 +0445 — +04:45
Thu, 02 Jun 2022 09:44:48 -0600 — -06:00
Thu, 02 Jun 2022 17:44:48 +0200 — CEST

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

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial