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
-
the
date
function,
which formats date and time -
the
date_default_timezone_set
function,
which sets the default time zone -
the
DateTimeZone
class,
which represents a time zone