I saved a value in MySQL using the bigint
column type for a date of August 11, 2013 - 11:55 PM (EST TIME)
This is saved as unix time in the bigint
column with value : 1376279700
I understand this to be epoch time since 1970 jan 01,01 00:00:00
in seconds. So I presumed that if I initialize DateTime using any timezone it should yield 08/11/2013 - 11:55 PM
( and whatever timezone used when initialized).
but given following code:
$time1 = new DateTime("@1376279700");
$time1->setTimezone("Europe/London");
echo "Created Europe/London - ".$time1->format(DateTime::RFC822);
and
$time2 = new DateTime("@1376279700");
$time2->setTimezone("America/New_York");
echo "Created America/New_York - ".$time2->format(DateTime::RFC822);
I get these values:
Created: Europe/London - Mon, 12 Aug 13 04:55:00 +0100
and
Created: America/New_York - Sun, 11 Aug 13 23:55:00 -0400
The Europe/London
timezone self-adjusts and somehow magically it knows that 1376279700
was created using EST timezone.
I am very confused here. Please shed some light here. I am trying to create a timezone aware function where start date of an event (08/11/2013 11:55 PM
) is used by my user's timezone.
The constructor ignores the timezone when you pass a Unix timestamp.
$ts = new DateTime('@946684800');
echo $ts->format('Y-m-d H:i:s') . PHP_EOL; // 2000-01-01 00:00:00
// Setting the time zone does nothing *here* . . .
$pac_tz = new DateTimeZone('America/Los_Angeles');
$ts = new DateTime('@946684800', $pac_tz);
echo $ts->format('Y-m-d H:i:s') . PHP_EOL; // 2000-01-01 00:00:00
// But you can ask for the "same" timestamp
// in a particular time zone.
$ts->setTimeZone($pac_tz);
echo $ts->format('Y-m-d H:i:s') . PHP_EOL; // 1999-12-31 16:00:00
echo '=============' .PHP_EOL;
$time1 = new DateTime("@1376279700");
echo $time1->format('Y-m-d H:i:s').PHP_EOL; // 2013-08-12 03:55:00
$time1_tz = new DateTimeZone("Europe/London");
$time1->setTimezone($time1_tz);
// If it's 2013-08-12 03:55:00 in UTC, what time is it in London?
// London isn't on UTC in the summer (now, as I write this).
echo "Created Europe/London - ".$time1->format(DateTime::RFC822) . PHP_EOL;
$time2 = new DateTime("@1376279700"); // Same as $time1
echo $time2->format('Y-m-d H:i:s').PHP_EOL;
$time2_tz = new DateTimeZone("America/New_York");
// If it's 2013-08-12 03:55:00 in UTC, what time is it in New York?
$time2->setTimezone($time2_tz);
echo "Created America/New_York - ".$time2->format(DateTime::RFC822) . PHP_EOL;
All the output . . .
2000-01-01 00:00:00
2000-01-01 00:00:00
1999-12-31 16:00:00
=============
2013-08-12 03:55:00
Created Europe/London - Mon, 12 Aug 13 04:55:00 +0100
2013-08-12 03:55:00
Created America/New_York - Sun, 11 Aug 13 23:55:00 -0400