Get time according to timezone php

The other answers set the timezone for all dates in your system. This doesn't always work well if you want to support multiple timezones for your users.

Here's the short version:

format('Y-m-d H:i:s');

Works in PHP >= 5.2.0

List of supported timezones: php.net/manual/en/timezones.php


Here's a version with an existing time and setting timezone by a user setting

format('Y-m-d H:i:s');

Here is a more verbose version to show the process a little more clearly

format('l, F j Y g:i:s A');     

// Output line break (for testing)
echo "\n
\n"; // Example user timezone (to show it can be used dynamically) $usersTimezone = 'America/New_York'; // Convert timezone $tz = new DateTimeZone($usersTimezone); $date->setTimeZone($tz); // Output date after echo $date->format('l, F j Y g:i:s A');

Libraries

  • Carbon — A very popular date library.
  • Chronos — A drop-in replacement for Carbon focused on immutability. See below on why that's important.
  • jenssegers/date — An extension of Carbon that adds multi-language support.

I'm sure there are a number of other libraries available, but these are a few I'm familiar with.


Bonus Lesson: Immutable Date Objects

While you're here, let me save you some future headache. Let's say you want to calculate 1 week from today and 2 weeks from today. You might write some code like:

format('Y-m-d') . "\n
"; echo "---\n
"; $oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days')); $twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days')); echo $today->format('Y-m-d') . "\n
"; echo $oneWeekFromToday->format('Y-m-d') . "\n
"; echo $twoWeeksFromToday->format('Y-m-d') . "\n
"; echo "\n
";

The output:

2017-02-11 
--- 
2017-03-04 
2017-03-04 
2017-03-04

Hmmmm... That's not quite what we wanted. Modifying a traditional DateTime object in PHP not only returns the updated date but modifies the original object as well.

This is where DateTimeImmutable comes in.

$today = new DateTimeImmutable();

echo $today->format('Y-m-d') . "\n
"; echo "---\n
"; $oneWeekFromToday = $today->add(DateInterval::createFromDateString('7 days')); $twoWeeksFromToday = $today->add(DateInterval::createFromDateString('14 days')); echo $today->format('Y-m-d') . "\n
"; echo $oneWeekFromToday->format('Y-m-d') . "\n
"; echo $twoWeeksFromToday->format('Y-m-d') . "\n
";

The output:

2017-02-11 
--- 
2017-02-11 
2017-02-18 
2017-02-25 

In this second example, we get the dates we expected back. By using DateTimeImmutable instead of DateTime, we prevent accidental state mutations and prevent potential bugs.

(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.