0

Either I'm losing my mind, or I've not got the faintest idea what I'm doing. I'm leaning towards the latter.

I'm trying to convert this: 1316826000, which I'm pretty confident should be Sat, 24 Sep 2011 01:00:00 GMT

http://www.onlineconversion.com/unix_time.htm confirms this.

http://www.unixtimestamp.com/index.php tells me 09 / 23 / 11 @ 8:00:00pm EST, so far so good. I happen to be in EST, this is the result I'd like to get back from PHP.

When I do date('l, M d, Y, h:ia', $iTime), I get: Friday, Sep 23, 2011, 12:00am, a full 20 hours off.

I've confirmed the server's time is correct using date('c'). date('c') output is: 2012-05-19T03:19:20+00:00. The server is in the central time zone, where it is currently 10:20pm. May 18.

echo date_default_timezone_get() outputs "GMT" (set somewhere else in the script using date_default_timezone_set('GMT'))

What am I missing? Nothing I've read so far can explain how I'm getting a result 20 hours behind what it should be. Were it an hour fast or slow, I could at least wrap my head around it being some sort of DST idiotry, but 20? Crazyness! Thanks for reading!

  • Have you tried `date_default_timezone_set`ting your timezone to EST? – deceze May 19 '12 at 03:32
  • 1316826000 may map to 8pm EST, but note that on September 23, 2011, the US was still observing daylight saving time, so the actual time zone in use would have been EDT, in which it was 9pm instead. FWIW, I'm unable to reproduce this problem in PHP 5.3.5: `print(date('l, M d, Y, h:ia', 1316826000))` prints `Friday, Sep 23, 2011, 09:00pm`. – Mark Reed May 19 '12 at 03:33

2 Answers2

1

Check what your php.ini says for date.timezone. In unix it is usually here: /etc/php.ini Then use a proper timezone recognized by PHP: http://www.php.net/manual/en/timezones.php

date.timezone = 'America/New_York'

Then reload your web server.

Unix time just means the number of seconds since epoch. Has nothing to do with timezones. Timezones simply add or subtract 1 hour (3600 seconds) from the unix time for each zone you move away from GMT. An example:

$userTimezone = new DateTimeZone('America/New_York');
$gmtTimezone = new DateTimeZone('GMT');
$myDateTime = new DateTime('2014-01-22 11:44', $gmtTimezone);
$offset = $userTimezone->getOffset($myDateTime);
echo $offset;

That will output: -14400 or 4 hours. Which is the difference between New York and GMT

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
arikin
  • 188
  • 11
0

Using some Java code with the Joda-Time 2.3 library, as I don't know PHP…

long m = 1316826000L;

DateTime dateTimeUtc = new DateTime( m * 1000L, DateTimeZone.UTC );
DateTime dateTimeNewYork = dateTimeUtc.toDateTime( DateTimeZone.forID( "America/New_York" ) );

System.out.println( "dateTimeUtc: " + dateTimeUtc );
System.out.println( "dateTimeNewYork: " + dateTimeNewYork );

I can tell you that 1316826000 seconds from the beginning of 1970 UTC/GMT (Unix Epoch) is…

dateTimeUtc: 2011-09-24T01:00:00.000Z
dateTimeNewYork: 2011-09-23T21:00:00.000-04:00

So, as the commenter stated, it would be 8 PM in EST but EST was not in effect on that day. DST (Daylight Saving Time) (idiocy, as you correctly noted) was in effect until November 9 of that year (2011). So the time of day is pushed forward one hour, to 9 PM.

In GMT/UTC, that means 1 AM in the morning of the next day.

Standard time in east coast US is 5 hours behind UTC/GMT. With DST it is 4 hours behind UTC/GMT (one hour closer).

Where you got confused:

  • Your time format/conversion in incorrect.
    I can't help with that as I don't know PHP.
  • You should be using a competent date-time library for this kind of work.
    Date-time work is complicated, tricky, confusing, and error-prone.
    This question discusses possibilities of Joda-Time (for Java) sorts of libraries for PHP.
  • You used three-letter time zone codes. Avoid these.
    Those codes are neither standardized nor unique -- there are common duplicates. Instead, use proper time zone names. In your case of east coast US, "America/New_York". Furthermore, in this case you confused the time zone area and rules (east coast US) with a particular application of those rules (EST). Saying "America/New_York" means "whatever time zone rules were in effect on that date, whereas saying "EST" (if interpreted to mean Eastern Standard Time in US) means "UTC-05:00". So either (a) use a time zone name such as "America/New_York", or (b) use a specific offset such as "-05:00".
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154