3
votes

I have a PHP application that needs to support timezones. Now I am storing date and time values in the database as integers. So here is what I do.

// now create the DateTime object for this time and user time zone
$dtime = new DateTime($date_time, new DateTimeZone($time_zone));
$timestamp = $dtime->format('U');

What I don't understand is even if the person is in New Zealand (Pacific/Auckland) and the application stores this date: 24-12-2010 00:00:00 using the above code the value comes back as: 1293102000

Then using the exact same date/time and setting the time zone to London (Europe/London) I get this timestamp:1293148800

Why are the timestamps different? I thought the timestamp would be the same number of seconds as its the same date from the Unix Epoch?

If they are different that means when someone is searching the database for all records between 24-12-2010 00:00:00 and 24-12-2010 23:59:59 I need to use the users timezone for the creation of the timestamps and I must use the timezone to firstly create the timestamps.

Help!

1

1 Answers

0
votes

Because timezone for New Zealand is +1300 UTC, where London is zero UTC, 13 x 3600seconds = 1293148800-1293102000

Since you already store as integer, you should always use zero UTC skip timezone when you construct $dtime

For testing

# date_create is procedural style
$obj =  date_create('24-12-2010 00:00:00', new DateTimeZone('europe/london'));
echo $obj->format('U');

$obj =  date_create('24-12-2010 00:00:00');
echo $obj->format('U');