user1547766
user1547766

Reputation: 475

Difference in PHP timestamp and mysql timestamp

I am really upset with the script I made, in which I did comparison of timestamps. I can see that the timestamps are totally different.

  1. For 2012-10-02 03:06:21, timestamp is: 1349190381
  2. For 2012-10-02 04:00:50, timestamp is: 1349150450

I can't understand why the second one timestamp is lower than the first, even if the time is bigger. How can I compare two dates?

I am getting these values by strtotime() function.

Clarification: I am using

$current_timestamp = strtotime(date('Y-m-d H:i:s'));

$till_date_timestamp = strtotime($database_object->till_date);

Upvotes: 1

Views: 437

Answers (3)

ddinchev
ddinchev

Reputation: 34673

Don't rely on strtotime! Also it's obvious that the timezone of PHP is different then the timezone of the database. Make sure the timezones are the same or get current/till timestamp like this (so that they are from one source):

SELECT UNIX_TIMESTAMP() AS 'Current' UNIX_TIMESTAMP(`datefield`) AS 'Till' FROM `table`

Upvotes: 2

Teena Thomas
Teena Thomas

Reputation: 5239

this is the result that i got. They differ from what you mentioned in the question.

Code:

    echo strtotime('2012-10-02 03:06:21'); //outputs: 1349147181
    echo "\n";
    echo strtotime('2012-10-02 04:00:50'); //outputs: 1349150450

Edit:

$till_date_timestamp = strtotime($database_object->till_date); make sure that $database_object->till_date is a string.

Upvotes: 1

Marc B
Marc B

Reputation: 360872

You sure you did it right?

mysql> select from_unixtime(1349190381), from_unixtime(1349150450);
+---------------------------+---------------------------+
| from_unixtime(1349190381) | from_unixtime(1349150450) |
+---------------------------+---------------------------+
| 2012-10-02 09:06:21       | 2012-10-01 22:00:50       |
+---------------------------+---------------------------+
1 row in set (0.00 sec)

php > echo date('r', 1349190381), "\n", date('r', 1349150450);
Tue, 02 Oct 2012 10:06:21 -0500
Mon, 01 Oct 2012 23:00:50 -0500

what string did you use in your strtotime calls? The function CAN mis-interpret inputs and should not be trusted on anything but well-formed and unambiguous date strings.

Upvotes: 2

Related Questions