• PHP Video Tutorials

PHP date_default_timezone_get() Function


Advertisements

Definition and Usage

The date_default_timezone_get() function returns the default timezone used by all the functions in a script.

If you set the time zone using the date_default_timezone_set() function. The date_default_timezone_get() returns the time zone value set previously, In you haven't set any default time zone value explicitly, this function will return the default timezone value of UTC.

Syntax

date_default_timezone_get()

Parameters

The date_default_timezone_get() function doesn't accept any parameters.

Return Values

This function returns a string value representing the default timezone.

PHP Version

This function was first introduced in PHP Version 5.1.0 and, works with all the later versions.

Example

Following example retrieves and prints the current default time zone −

Live Demo
<?php
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
?>

This will produce following result −

Default timezone: UTC

Example

In the following example we are setting the default time zone using the date_default_timezone_set() function −

Live Demo
<?php
   //setting the timezone
   $tz = 'Indian/Mahe';   
   date_default_timezone_set($tz);
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
?>

If you retrieve the default time zone using date_default_timezone_get it will return the value you have set previously −

Default timezone: Indian/Mahe

Example

Following example print the default time zone and its abbreviation −

<?php
   //setting the timezone
   $tz = 'Asia/Kolkata';   
   date_default_timezone_set($tz);
   //Retrieving the default timezone
   $timeZone = date_default_timezone_get();
   print("Default timezone: ".$timeZone);
   print("\n");

   //Getting abbreviation
   //$abbvr = $timeZone.date('e').date(T);
   print("Abbreviation: " .date('T'));
?>

This will produce following result −

Default timezone: Asia/Kolkata
Abbreviation: IST

Example

<?php
   echo "Old time zone is ". date_default_timezone_get();
   $timeZone = 'America/Costa_Rica';
   
   if( date_default_timezone_set( $timeZone) ){
      # Now get this time zone.
      echo "New time zone is ". date_default_timezone_get();
   }
?>

This will produce the following result −

Old time zone is America/Denver
New time zone is America/Costa_Rica
php_function_reference.htm
Advertisements