PHP Function Reference

PHP date_timezone_get() Function



The PHP date_timezone_get() function returns the time zone relative to given DateTime. This function is an alias of DateTime::getTimezone() method.

Note: This method is defined under DateTimeInterface interface. DateTime and DateTimeImmutable classes inherit this method from DateTimeInterface interface.

Syntax

//Object-oriented style
public DateTime::getTimezone()
public DateTimeImmutable::getTimezone()
public DateTimeInterface::getTimezone()

//Procedural style
date_timezone_get(object)

Parameters

object Required. For procedural style only: A DateTime object returned by date_create().

Return Value

Returns a DateTimeZone object on success or false on failure.

Example: using both styles

The example below shows the usage of date_timezone_get() function.

<?php
  //creating a DateTime object
  $date = date_create(null, new DateTimeZone('Europe/London'));

  //getting the timezone using Object-oriented style
  $tz = $date->getTimezone();
  echo "TimeZone: ".$tz->getName()."\n";

  //getting the timezone using Procedural style
  $tz = date_timezone_get($date);
  echo "TimeZone: ".timezone_name_get($tz)."\n";
?>

The output of the above code will be:

TimeZone: Europe/London
TimeZone: Europe/London

Example: Timezone of a DateTime object

Consider the example below where the timezone of a DateTime object is returned using this function.

<?php
  //creating a DateTime object
  $date = date_create(null, new DateTimeZone('Europe/London'));

  //getting the timezone
  $tz = date_timezone_get($date);
  echo "Initial TimeZone: ".timezone_name_get($tz)."\n";

   //setting the timezone to a new value
   date_timezone_set($date, new DateTimeZone('America/Chicago'));

  //getting the timezone
  $tz = date_timezone_get($date);
  echo "Final TimeZone: ".timezone_name_get($tz)."\n";
?>

The output of the above code will be:

Initial TimeZone: Europe/London
Final TimeZone: America/Chicago

Example: Timezone of a DateTimeImmutable object

Similarly, this function can be used with DateTimeImmutable object also. Consider the example below:

<?php
  //creating a DateTimeImmutable object
  $date = date_create_immutable(null, new DateTimeZone('Europe/London'));

  //getting the timezone
  $tz = date_timezone_get($date);
  echo "TimeZone: ".timezone_name_get($tz)."\n";
?>

The output of the above code will be:

TimeZone: Europe/London

❮ PHP Date and Time Reference