PHP Function Reference

PHP date_default_timezone_set() Function



The PHP date_default_timezone_set() function sets the default timezone used by all date/time functions.

Note: Instead of using this function to set the default timezone in the script, you can also use the INI setting date.timezone to set the default timezone.

Syntax

date_default_timezone_set(timezoneId)

Parameters

timezoneId Required. Specify the timezone to use, like "UTC" or "Europe/London". See List of Supported Timezones for a list of supported timezones.

Return Value

Returns false if the timezoneId is not valid, or true otherwise.

Example: Setting the default timezone

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

<?php
  //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 "Default timezone: $get_tz";
?>

The output of the above code will be:

Default timezone: Europe/London

Example: Changing the default timezone

In the example below default timezone is checked before and after the use of this function.

<?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
  //setting the default timezone
  $set_tz = "Europe/London";
  date_default_timezone_set($set_tz);

  //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.

❮ PHP Date and Time Reference