English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP date_default_timezone_get() Function

PHP Date & Time Function Manual

The date_default_timezone_get() function retrieves the default time zone used by all date-time functions in the script

Definition and Usage

date_default_timezone_get()The function returns the default time zone used by all date-time functions in the script.

If you use the date_default_timezone_set() function to set the timezone, date_default_timezone_get() returns the previously set timezone value. If you have not explicitly set any default timezone value, this function will return the default timezone value of UTC.

Syntax

date_default_timezone_get()

Parameters

The date_default_timezone_get() function does not accept any parameters.

Return value

This function returns a string value representing the default timezone.

PHP version

This function was initially introduced in PHP version5.1is introduced in PHP version 5.2.0 and can be used in all higher versions.

Online example

The following example retrieves and prints the current default timezone-

<?php
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
?>
Test and see‹/›

Output result

Default timezone: UTC

Online example

In the following example, we usedate_default_timezone_set()function to set the default timezone-

<?php
   //Set timezone
   $tz = 'Asia/Shanghai';   
   date_default_timezone_set($tz);
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
?>
Test and see‹/›

If you usedate_default_timezone_getRetrieve the default timezone, it will return the value you set before-

Default timezone: Asia/Shanghai

Online example

The following example prints the default timezone and its abbreviation-

<?php
   //Set timezone
   $tz = 'Asia/Kolkata';   
   date_default_timezone_set($tz);
   //Retrieve the default timezone
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
   print("\n");
   //Get abbreviation
   //$abbvr = $timeZone.date('e').date(T);
   print("Abbreviation: ". date('T'));
?>
Test and see‹/›

Output result

Default timezone: Asia/Kolkata
Abbreviation: IST

Online example

Get the old timezone and set a new timezone

<?php
   echo "The old timezone is ". date_default_timezone_get();
   $timeZone = 'America/Costa_Rica';
   
   if( date_default_timezone_set( $timeZone) ) {
      # Now get the timezone.
      echo "The new timezone is ". date_default_timezone_get();
   }
?>
Test and see‹/›

Output result:

The old timezone is UTC
The new timezone is America/Costa_Rica