228 of 410 menu

The DateTimeZone Class

The DateTimeZone class allows you to work with time zones in PHP. It accepts a string with a time zone identifier in its constructor.

Syntax

new DateTimeZone(string $timezone);

Example

Let's create a time zone object for Moscow:

<?php $timezone = new DateTimeZone('Europe/Moscow'); print_r($timezone); ?>

Code execution result:

DateTimeZone Object ( 'timezone_type' => 3 'timezone' => Europe/Moscow )

Example

Usage with the DateTime class:

<?php $timezone = new DateTimeZone('America/New_York'); $date = new DateTime('now', $timezone); echo $date->format('Y-m-d H:i:s'); ?>

Code execution result (current time in New York):

'2025-06-15 14:30:00'

Example

Getting a list of all available time zones:

<?php $timezones = DateTimeZone::listIdentifiers(); print_r(array_slice($timezones, 0, 5)); ?>

Code execution result:

[ 'Africa/Abidjan' 'Africa/Accra' 'Africa/Addis_Ababa' 'Africa/Algiers' 'Africa/Asmara' ]

Example

Getting the time zone offset from UTC:

<?php $timezone = new DateTimeZone('Asia/Tokyo'); echo $timezone->getOffset($date) / 3600; ?>

Code execution result:

9

See Also

byenru