CJ7
CJ7

Reputation: 23295

How to use timezones with php dates?

I have a PHP page that is pulling in a date string.

eg. 2012-11-10 11:37:06

I know that this time is in a specific timezone.

I want to store this date in UTC time in a MySQL date field so that it can later be converted to a client's local time.

How can I do this in PHP?

Upvotes: 1

Views: 142

Answers (1)

John Carter
John Carter

Reputation: 55369

To convert timezones, use the DateTime class http://www.php.net/manual/en/class.datetime.php

// Create a DateTime object with your local time and local timezone.
$d = new DateTime('2012-11-10 11:37:06', new DateTimeZone('Pacific/Auckland'));

// Convert to UTC
$d->setTimeZone(new DateTimeZone('UTC'));

// outputs 2012-11-09 22:37:06
echo $d->format('Y-m-d H:i:s');

Upvotes: 3

Related Questions