PHP Date() With Timezone

PHP date() with timezone?

For such task, you should really be using PHP's DateTime class. Please ignore all of the answers advising you to use date() or date_set_time_zone, it's simply bad and outdated.

I'll use pseudocode to demonstrate, so try to adjust the code to suit your needs.

Assuming that variable $tz contains string name of a valid time zone and variable $timestamp contains the timestamp you wish to format according to time zone, the code would look like this:

$tz = 'Europe/London';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
echo $dt->format('d.m.Y, H:i:s');

DateTime class is powerful, and to grasp all of its capabilities - you should devote some of your time reading about it at php.net. To answer your question fully - yes, you can adjust the time zone parameter dynamically (on each iteration while reading from db, you can create a new DateTimeZone() object).

How to set time zone with DateTime object?

look at DateTimeZone

$date = new DateTime('now',new DateTimeZone("Europe/Rome"));

PHP timestamps & timezone configuration

Using time() is the same as strtotime("now") and you do not need to worry about converting the timezone of the timestamp, as the timestamp has no timezone:

Does PHP time() return a GMT/UTC Timestamp?

time returns a UNIX timestamp, which is timezone independent. Since
a UNIX timestamp denotes the seconds since 1970 UTC you could say it's
UTC, but it really has no timezone.

You can then store that timestamp in your database. When you retrieve it you can convert it to the users timezone. With something like this:

date_default_timezone_set('UTC');

$timestamp = '1429066967';
//Supported Timezones: http://php.net/manual/en/timezones.php
$userTimezone = 'America/Los_Angeles';

$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');

echo $date;

Outputs: 2015-04-14 20:02:47

But if you only have the UTC offset you could try this:

date_default_timezone_set('UTC');

$timestamp = '1429066967';
$offset = -8;
$userTimezone = timezone_name_from_abbr("", $offset*3600, false);


$dt = new DateTime();
// Set the timestamp
$dt->setTimestamp($timestamp);
// Set the timezone
$dt->setTimezone(new DateTimeZone($userTimezone));
// Format the date
$date = $dt->format('Y-m-d H:i:s');

echo $date;

Which also outputs: 2015-04-14 20:02:47

Changing timezone of a retrieved date in php

Your input datetime is in UTC, not user's timezone. So first you must create datetime object in UTC, and then set/change timezone to user's :

$dt = new DateTime('2013-09-15 08:45:00', new DateTimeZone('UTC'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 08:45:00
[timezone_type] => 3
[timezone] => UTC
)
*/

Now you have datetime in UTC timezone. If you wish to change timezone, just call ->setTimezone() on DateTime object :

$dt->setTimezone(new DateTimeZone('Europe/Berlin'));
print_r($dt);
/*
DateTime Object
(
[date] => 2013-09-15 10:45:00
[timezone_type] => 3
[timezone] => Europe/Berlin
)
*/

p.s. because input 2013-09-15 08:45:00 is in standard datetime format, you don't need to use DateTime::createFromFormat.

Get Current DateTime in PHP for Spacific Timezone

Unix Timestamps are always in UTC and therefore always the same. Try using the c formatter to see the differences:

$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";

$now = new DateTime(null, new DateTimeZone('America/New_York'));
echo "America/New_York = ". $now->format('c') . "\r\n";

$now = new DateTime(null, new DateTimeZone('Europe/Stockholm'));
echo "Europe/Stockholm = ". $now->format('c') . "\r\n";

$now = new DateTime(null, new DateTimeZone('Asia/Muscat'));
echo "Asia/Muscat = ".$now->format('c') . "\r\n";

$current_time = date('Y-m-d H:i:s');
echo "The current server time is: " . $current_time . "\r\n";

The current server time is: 2015-04-04 22:26:56
America/New_York = 2015-04-04T18:26:56-04:00
Europe/Stockholm = 2015-04-05T00:26:56+02:00
Asia/Muscat = 2015-04-05T02:26:56+04:00
The current server time is: 2015-04-04 22:26:56

Demo

PHP - output of date('now') is a ahead of local time by 6 hours, but time zone is set to correct local zone zone?

There are a few different ways you can choose to handle this:

  • You can use date_default_timezone_set to override Wordpress's time zone setting.

  • You can use the PHP DateTime class instead of the date function, which accepts a time zone in the constructor

  • You can change Wordpress's time zone setting in the WP Admin / Settings / General section:

screenshot



Related Topics



Leave a reply



Submit