Matthew
Matthew

Reputation: 15642

php - datetimezone

I'm using functions with the datetime object that require a datetimezone object as an argument. I see a list of timezones here:

http://www.php.net/manual/en/class.datetimezone.php

but there's not things like 'est'. How could I create a 'datetimezone' object from EST?

Upvotes: 1

Views: 9410

Answers (4)

R.J. Steinert
R.J. Steinert

Reputation: 189

A very verbose loop. The construct function for the DateTime class isn't working properly for me but this works.

$date = "2011/03/20";
$date = explode("/", $date);

$time = "07:16:17";
$time = explode(":", $time);

$tz_string = "America/Los_Angeles"; // Use one from list of TZ names http://us2.php.net/manual/en/timezones.php
$tz_object = new DateTimeZone($tz_string);

$datetime = new DateTime();
$datetime->setTimezone($tz_object);
$datetime->setDate($date[0], $date[1], $date[2]);
$datetime->setTime($time[0], $time[1], $time[2]);

print $datetime->format('Y/m/d H:i:s');

?>

Upvotes: 1

Michael Konietzka
Michael Konietzka

Reputation: 5499

Be aware of that some TimeZone abbreviations like EST are not unique. See http://www.timeanddate.com/library/abbreviations/timezones/.

The usage of EST is not recommended, see http://www.php.net/manual/en/timezones.others.php.

Use for example America/New_York instead.

Upvotes: 0

Luis Alvarado
Luis Alvarado

Reputation: 9386

In http://www.php.net/manual/en/timezones.php you can find in the OTHER section the EST there.

To create one use date_default_timezone_set('EST');

To make sure you have it do echo date_default_timezone_get();

Upvotes: 2

rik
rik

Reputation: 8612

$tz = new DateTimeZone('EST');

Upvotes: 6

Related Questions