user2917204
user2917204

Reputation: 1353

PHP date() with timezone?

So I've checked the list of supported time zones in PHP and I was wondering how could I include them in the date() function? Thanks!

I don't want a default timezone, each user has their timezone stored in the database, I take that timezone of the user and use it. How? I know how to take it from the database, not how to use it, though.

Upvotes: 116

Views: 275269

Answers (13)

best-web-tools.com
best-web-tools.com

Reputation: 101

You could also crate a DateTime object by providing a timestamp as string in the constructor with a leading @ sign.

$dt = new DateTime('@123456789');
$dt->setTimezone(new DateTimeZone('America/New_York'));
echo $dt->format('F j, Y - G:i');

See the documentation about compound formats: https://www.php.net/manual/en/datetime.formats.compound.php

Upvotes: 1

Andrew
Andrew

Reputation: 20071

You can set the time zone on an existing object using setTimezone() and then you can set the timestamp:

$dt = new DateTime();
$dt->setTimezone(new DateTimeZone('America/New_York'));
$dt->setTimestamp(123456789);

echo $dt->format('F j, Y @ G:i');

Upvotes: 24

N.B.
N.B.

Reputation: 14060

For such a 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.

$dt = new DateTime("now", new DateTimeZone('Europe/London')); 
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).

Upvotes: 189

B. Martin
B. Martin

Reputation: 1163

I had a weird problem on a hosting. The timezone was set correctly, when I checked it with the following code.

echo ini_get('date.timezone');

However, the time it returned was UTC.

The solution was using the following code since the timezone was set correctly in the PHP configuration.

date_default_timezone_set(ini_get('date.timezone'));

Upvotes: -1

Kar.ma
Kar.ma

Reputation: 857

Based on other answers I built a one-liner, where I suppose you need current date time. It's easy to adjust if you need a different timestamp.

$dt = (new DateTime("now", new DateTimeZone('Europe/Rome')))->format('d-m-Y_His');

Upvotes: 6

Mike.Stanley
Mike.Stanley

Reputation: 199

U can just add, timezone difference to unix timestamp. Example for Moscow (UTC+3)

echo date('d.m.Y H:i:s', time() + 3 * 60 * 60);

Upvotes: 8

emmanuel
emmanuel

Reputation: 151

this works perfectly in 2019:

date('Y-m-d H:i:s',strtotime($date. ' '.$timezone)); 

Upvotes: 2

Shatadip Majumder
Shatadip Majumder

Reputation: 132

I have created this very straightforward function, and it works like a charm:

function ts2time($timestamp,$timezone){ /* input: 1518404518,America/Los_Angeles */            
        $date = new DateTime(date("d F Y H:i:s",$timestamp));
        $date->setTimezone(new DateTimeZone($timezone));
        $rt=$date->format('M d, Y h:i:s a'); /* output: Feb 11, 2018 7:01:58 pm */
        return $rt;
    }

Upvotes: 1

Suresh Kamrushi
Suresh Kamrushi

Reputation: 16076

If I understood correct,You need to set time zone first like:

date_default_timezone_set('UTC');

And than you can use date function:

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');

Upvotes: 50

dzona
dzona

Reputation: 3751

Try this. You can pass either unix timestamp, or datetime string

public static function convertToTimezone($timestamp, $fromTimezone, $toTimezone, $format='Y-m-d H:i:s') 
    {
        $datetime = is_numeric($timestamp) ?
                    DateTime::createFromFormat ('U' , $timestamp, new DateTimeZone($fromTimezone)) :
                    new DateTime($timestamp, new DateTimeZone($fromTimezone));

        $datetime->setTimezone(new DateTimeZone($toTimezone));

        return $datetime->format($format);
    }

Upvotes: 2

Yatin Trivedi
Yatin Trivedi

Reputation: 669

You can replace database value in date_default_timezone_set function, date_default_timezone_set(SOME_PHP_VARIABLE); but just needs to take care of exact values relevant to the timezones.

Upvotes: -2

user3049608
user3049608

Reputation: 173

It should like this:

date_default_timezone_set('America/New_York');

Upvotes: 13

ceejayoz
ceejayoz

Reputation: 179994

Use the DateTime class instead, as it supports timezones. The DateTime equivalent of date() is DateTime::format.

An extremely helpful wrapper for DateTime is Carbon - definitely give it a look.

You'll want to store in the database as UTC and convert on the application level.

Upvotes: 20

Related Questions