Trying to understand timestamps and time zones in PHP

I have a PHP application that should support time zones. Now I store the 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 do not understand, even if the person is in New Zealand (Pacific / Auckland), and the application stores this date: 12-24-2010 00:00:00, using the code above, the value is returned as: 1293102000

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

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

If they are different from each other, this means that when someone searches the database for all records between 12-24-2010 00:00:00 and 12-24-2010 23:59:59 I need to use the users time zone for create timestamps, and I have to use the time zone to create timestamps.

Help!

+3
source share
1 answer

Since the time zone for New Zealand is +1300 UTC, where London is zero UTC, 13 x 3600seconds = 1293148800-1293102000

Since you already store an integer, you should always use zero UTC to skip the time zone when building$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');
0
source

Source: https://habr.com/ru/post/1781964/


All Articles