date_default_timezone_get
(PHP 5 >= 5.1.0)
date_default_timezone_get — Gets the default timezone used by all date/time functions in a script
Description
In order of preference, this function returns the default timezone by:
-
Reading the timezone set using the date_default_timezone_set() function (if any)
-
Prior to PHP 5.4.0 only: Reading the TZ environment variable (if non empty)
-
Reading the value of the date.timezone ini option (if set)
-
Prior to PHP 5.4.0 only: Querying the host operating system (if supported and allowed by the OS). This uses an algorithm that has to guess the timezone. This is by no means going to work correctly for every situation. 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, date_default_timezone_get() will return a default timezone of UTC.
Return Values
Returns a string.
Changelog
Version | Description |
---|---|
5.4.0 | The TZ environment variable is no longer used to guess the timezone. |
5.4.0 | The timezone is no longer guessed from information available through the operating system as the guessed timezone can not be relied on. |
Examples
Example #1 Getting the default timezone
<?php
date_default_timezone_set('Europe/London');
if (date_default_timezone_get()) {
echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}
if (ini_get('date.timezone')) {
echo 'date.timezone: ' . ini_get('date.timezone');
}
?>
The above example will output something similar to:
date_default_timezone_set: Europe/London date.timezone: Europe/London
Example #2 Getting the abbreviation of a timezone
<?php
date_default_timezone_set('America/Los_Angeles');
echo date_default_timezone_get() . ' => ' . date('e') . ' => ' . date('T');
?>
The above example will output:
America/Los_Angeles => America/Los_Angeles => PST
See Also
- date_default_timezone_set() - Sets the default timezone used by all date/time functions in a script
- List of Supported Timezones
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с датой и временем
- Дата и Время
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
Коментарии
If you are upgrading an ancient script which was written pre PHP 5.4 please be aware that the configured server timezone was used to guess the timezone; this was changed to UTC with PHP 5.4!