I’ve been using PHP’s strtotime and MySQL’s UNIX_TIMESTAMP functions in my app, to convert dates into timestamps. PHP and MySQL are running on my local machine, and these functions generally return the same result, as I expect them to:
$ php
<?php echo strtotime("2011-06-02"); ?>
1307001600
mysql> SELECT UNIX_TIMESTAMP("2011-06-02") ts;
+------------+
| ts |
+------------+
| 1307001600 |
+------------+
But, sorta by chance, I happened to notice that when I entered 1983-01-01 as the date, the results were no longer equal:
$ php
<?php echo strtotime("1983-01-01"); ?>
410263200
mysql> SELECT UNIX_TIMESTAMP("1983-01-01") ts;
+-----------+
| ts |
+-----------+
| 410256000 |
+-----------+
As you can see, PHP returned 410263200, while MySQL returned 410256000 – a difference of 7200 seconds.
This got me curious, and I wanted to know on what date the timestamps were no longer equivalent, so I wrote a little program that starts with today’s date (in Y-m-d format), uses PHP’s strtotime and MySQL’s UNIX_TIMESTAMP and compares the results. It then subtracts 1 day from each value and loops until they’re no longer equal.
The result:
1983-10-29
On October 29, 1983, for some reason, strtotime and UNIX_TIMESTAMP return values that differ by 7200 seconds.
Any ideas?
Thanks for reading.
You say you’re on Alaskan time. From http://www.statoids.com/tus.html:
So, I’d guess that this is because your timezone changed by two hours (7200 seconds = 2 hours) on 30th October 1983.
I’d guess that MySQL’s UNIX_TIMESTAMP is using a different timezone from your Alaskan setting in PHP, which may be either the server default, or dependent on your connection settings, so these diverge when you hit that date.
You may be able to glean more information by asking MySQL what its timezone setting is on that connection with
SELECT @@global.time_zone, @@session.time_zone;; see this answer for more info on the output and how to interpret it.