244 of 410 menu

The getTimezone Method of the DateTime Class

The getTimezone method returns a DateTimeZone object, representing the time zone set for the DateTime object. If the time zone is not set, the default time zone will be returned.

Syntax

public DateTime::getTimezone(): DateTimeZone|false

Example

Get the time zone for the current date:

<?php $date = new DateTime(); $timezone = $date->getTimezone(); echo $timezone->getName(); ?>

Code execution result (depends on server settings):

'Europe/Moscow'

Example

Set the time zone and check it:

<?php $date = new DateTime('now', new DateTimeZone('America/New_York')); $timezone = $date->getTimezone(); echo $timezone->getName(); ?>

Code execution result:

'America/New_York'

See Also

byenru