tags:

views:

25

answers:

2

Using PHP 5.2.5 I was working with some DateTime objects and noticed a time that seemed off. The problem I'm having may be related to timezones, but I'm not sure - I'm creating a DateTime from a Unix Time Stamp and getting different/unexpected results depending on how I output it.

I created the following to easily illustrate the "issue":

$timezone = new DateTimeZone('America/Chicago'); 
$now = time(); 
$now_datetime = new DateTime('@' . $now, $timezone); 
echo phpversion() . "\n\n"; 
echo $now . "\n"; 
echo $now_datetime->format('U') . "\n\n"; 
echo date('g:i:sa', $now) . "\n";
echo $now_datetime->format('g:i:sa') . "\n\n";

This outputs the following:

5.2.5

1287676530
1287676530

10:55:30am
3:55:30pm

I'm currently in the correct timezone, and the server shows the "right" time (10am) when using the date() function to output a formatted date, as well as 'America/Chicago' being the default timezone on that machine. But, when outputting values via DateTime::format(), the times are very different.

I added the ->format('U') just to verify that it was holding the correct timestamp.

So, I'm probably doing something wrong or I have the wrong expectations. So what am I missing?

It seems like a timezone issue with DateTime, but if that's the case, why does it show "now" in America/Chicago as ... wrong?

+1  A: 

The manual on DateTime's constructor has the answer:

The $timezone parameter and the current timezone are ignored when the $time parameter either is a UNIX timestamp (e.g. @946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00).

This means that your timestamp is treated as a GMT one, explaining the 7 hours' difference.

Pekka
I counted 5 hours, but either way: If the timezone is discarded (which I did miss) how can I tell the DateTime to output that time in my current time? **EDIT**: Upon considering @Wrikken's answer, I realized how to fix this. Thanks!
anonymous coward
@anon you're right, it's 5 of course. Hmm... I would feed it with a `date()` output instead: `date("d.m.Y H:i:s");` and add the time zone offset manually: `+0500` (or `-0500`, whatever applies) that way, the internal time in the `Datetime` is correct
Pekka
@Pekka: @Wrikken pointed out that the timezone has to be set explicitly. This *sort of* makes sense, as you pointed out that PHP discards any incoming timezone information when providing a timestamp to the DateTime constructor. My problem was that I was assuming the provided timezone information would stick around for later calculation (output via `format()`), etc. Thanks!
anonymous coward
+1  A: 

The timezone you 'insert' into DateTime is the timezone of the string, which may not be your current timezone, so PHP can calculate the actual datetime, not the timezone it uses to format your output. If you want it to output the time in a specific timezone, use $now_datetime->setTimezone($timezone).

Wrikken
This answer actually led me to the solution, after @Pekka provided clarification. Since PHP discards the timezone information when supplying a timestamp to the constructor, it (obviously?) doesn't remember that timezone for later output. Thus, as you said, I have to explicitly use `setTimezone()` before attempting to get output. This seems a little weird to me, but it works correctly.
anonymous coward
If you need it in your whole application (not just per-TimeDate-object basis), call `date_default_timezone_set('America/Chicago');`
Wrikken