PHP date_default_timezone_set() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, date_default_timezone_set() sets the default timezone used by all date and time functions in your script. It returns true on success and false if the timezone name is not valid.

Once you set the timezone, functions such as date(), mktime() and strtotime(), and any DateTime created without a timezone, use it. The change lasts until the script ends or you change it again. The timezone name must be a valid identifier like Asia/Kolkata, not an abbreviation like IST.

Syntax

Call date_default_timezone_set() with the values you need. Start with this line.

PHP
date_default_timezone_set('Asia/Kolkata');
CallWhat it does
date_default_timezone_set('UTC')Uses Coordinated Universal Time. This is a good default for servers.
date_default_timezone_set('Asia/Kolkata')Uses Indian Standard Time, which is UTC+05:30.
date_default_timezone_set('America/New_York')Uses US Eastern time and switches between EST and EDT automatically.

Use region and city identifiers such as Europe/Berlin. They know about daylight saving time. You can list the valid names with timezone_identifiers_list(). An invalid name returns false, and PHP also raises a notice, so check the result in production code.

Library declarations
PHP
date_default_timezone_set(string $timezoneId): bool

Parameters

ParameterDescription
timezoneIdRequired. The timezone identifier, such as UTC, Europe/London or Australia/Sydney. Abbreviations like EST work in some contexts but are not recommended.

Return Value

Return ValueDescription
trueThe timezone was valid and is now the default.
falseThe identifier is not valid. The old default stays active and PHP raises a notice.

PHP Examples

Example 1 — One Moment, Three Timezones

A Unix timestamp is the same everywhere, but date() prints it using the default timezone. Change the timezone to see different clock times.

PHP
<?php
$ts = strtotime('2024-01-15 10:30:00 UTC');

date_default_timezone_set('UTC');
echo date('Y-m-d H:i', $ts), PHP_EOL;

date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i', $ts), PHP_EOL;

date_default_timezone_set('Australia/Sydney');
echo date('Y-m-d H:i', $ts), PHP_EOL;

How It Works

  1. The timestamp is created from a string that includes UTC, so it never changes.
  2. With UTC as default, date() prints 10:30.
  3. India is UTC+05:30, so the same moment prints as 16:00.
  4. Sydney uses daylight time in January (UTC+11), so it prints 21:30.

Example 2 — Return Value and Invalid Names

The function returns true for a valid identifier and false for an invalid one. The @ operator hides the notice that PHP raises for a bad name.

PHP
<?php
var_dump(date_default_timezone_set('Asia/Tokyo'));
var_dump(@date_default_timezone_set('Tokyo'));
echo date_default_timezone_get(), PHP_EOL;

How It Works

  1. Asia/Tokyo is a valid identifier, so the first call returns true.
  2. 'Tokyo' alone is not a valid identifier, so the second call returns false.
  3. The failed call does not change anything, so the default timezone is still Asia/Tokyo.
  4. In real code, check the return value instead of hiding the notice and forgetting about it.

Example 3 — Validating a Name and Daylight Saving Time

Check the name against timezone_identifiers_list(), set it, and see how the same clock time changes with daylight saving.

PHP
<?php
$id = 'Europe/Berlin';

if (in_array($id, timezone_identifiers_list(), true)) {
    date_default_timezone_set($id);
    echo "Using: ", date_default_timezone_get(), PHP_EOL;
}

$winter = strtotime('2024-01-15 12:00:00 UTC');
$summer = strtotime('2024-07-15 12:00:00 UTC');

echo date('H:i T', $winter), PHP_EOL;
echo date('H:i T', $summer), PHP_EOL;

How It Works

  1. in_array() checks that the name exists in the list of valid identifiers.
  2. Only a valid name reaches date_default_timezone_set(), and the getter confirms it.
  3. In January Berlin is on CET, one hour ahead of UTC, so noon UTC prints as 13:00.
  4. In July Berlin is on CEST, two hours ahead of UTC, so noon UTC prints as 14:00.

Frequently Asked Questions

It sets the default timezone for all date and time functions in the current script. After calling it, date(), strtotime() and new DateTime() use that timezone.
The function returns false, raises a notice, and keeps the previous timezone. Use a name from timezone_identifiers_list() to avoid this.
Use Asia/Kolkata. Identifiers with a region and city are unambiguous and handle daylight saving rules, while abbreviations can mean different zones.
No. It only lasts for the current script run. To change it for every script, set date.timezone in php.ini.
No. It only affects PHP. Your database connection has its own timezone setting, so set both when you store or compare dates.

Next: date_diff()

date_diff() calculates the difference between two dates and returns it as a DateInterval o.

date_diff() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful