PHP Function Reference

PHP date_default_timezone_get() Function



The PHP date_default_timezone_get() function gets the default timezone used by all date/time functions. This function returns the default timezone using the following order of preference:

  • Reading the timezone set using the date_default_timezone_set() function (if any)
  • Reading the value of the date.timezone ini option (if set)
  • A warning is shown when this stage is reached. Do not rely on it to be guessed correctly, and set date.timezone to the correct timezone instead.

If none of the above succeed, this function returns a default timezone of UTC.

Syntax

date_default_timezone_set()

Parameters

No parameter is required.

Return Value

Returns the timezone as a string

Example: Getting the default timezone

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

<?php
  //getting the default timezone
  $get_tz = date_default_timezone_get();
  echo "Default timezone: $get_tz";
?>

The output of the above code will be:

Default timezone: UTC

Example: Default timezone after setting

In the example below default timezone is checked before and after setting it.

<?php
  //getting the default timezone
  $get_tz = date_default_timezone_get();
  echo "Initial Default timezone: $get_tz \n";

  //setting the default timezone
  $set_tz = "Europe/London";
  date_default_timezone_set($set_tz);

  //getting the default timezone
  $get_tz = date_default_timezone_get();
  echo "Final Default timezone: $get_tz \n";
?>

The output of the above code will be:

Initial Default timezone: UTC 
Final Default timezone: Europe/London 

Example: Comparing the default timezone with ini-set timezone

In the example below default timezone is compared with ini-set timezone.

<?php
  //getting the default timezone
  $get_tz = date_default_timezone_get();

  //comparing the default timezone with ini-set timezone
  if (strcmp($get_tz, ini_get("date.timezone"))){
    echo "Script timezone differs from ini-set timezone.";
  } else {
    echo "Script timezone and ini-set timezone match.";
  }
?>

The output of the above code will be:

Script timezone differs from ini-set timezone.

Example: Getting the abbreviation of a timezone

This function can be used to get the abbreviation of a timezone. Consider the example below:

<?php
  //setting the default timezone
  $set_tz = "America/Chicago";
  date_default_timezone_set($set_tz);

  //getting the default timezone
  $get_tz = date_default_timezone_get();
  echo "Default timezone: $get_tz \n";

  //getting the abbreviation of the timezone
  echo date('e') . ' => ' . date('T');
?>

The output of the above code will be:

Default timezone: America/Chicago 
America/Chicago => CDT

❮ PHP Date and Time Reference