How to display time according to timezone in php?

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

date_default_timezone_get Gets the default timezone used by all date/time functions in a script

Description

date_default_timezone_get(): string

  • Reading the timezone set using the date_default_timezone_set() function (if any)

  • Reading the value of the date.timezone ini option (if set)

If none of the above succeed, date_default_timezone_get() will return a default timezone of UTC.

Parameters

This function has no parameters.

Return Values

Returns a string.

Examples

Example #1 Getting the default timezone

date_default_timezone_set('Europe/London');

if (

date_default_timezone_get()) {
    echo 
'date_default_timezone_set: ' date_default_timezone_get() . '';
}

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

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

There are no user contributed notes for this page.

date_timezone_set

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DateTime::setTimezone -- date_timezone_setSets the time zone for the DateTime object

Parameters

object

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

timezone

A DateTimeZone object representing the desired time zone.

Return Values

Returns the DateTime object for method chaining. The underlaying point-in-time is not changed when calling this method.

Examples

Example #1 DateTime::setTimeZone() example

Object-oriented style

$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo 
$date->format('Y-m-d H:i:sP') . "\n";$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo 
$date->format('Y-m-d H:i:sP') . "\n";
?>

Procedural style

$date date_create('2000-01-01'timezone_open('Pacific/Nauru'));
echo 
date_format($date'Y-m-d H:i:sP') . "\n";date_timezone_set($datetimezone_open('Pacific/Chatham'));
echo 
date_format($date'Y-m-d H:i:sP') . "\n";
?>

The above examples will output:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

See Also

  • DateTimeImmutable::setTimezone() - Sets the time zone
  • DateTime::getTimezone() - Return time zone relative to given DateTime
  • DateTimeZone::__construct() - Creates new DateTimeZone object

There are no user contributed notes for this page.

How can I get timezone in PHP?

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

What timezone does PHP use?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts.

How does PHP manage different time zones?

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

How do I find my server time zone?

The currently configured timezone is set in the /etc/timezone file. To view your current timezone you can cat the file's contents. Another method is to use the date command. By giving it the argument +%Z , you can output your system's current time zone name.