1

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);
3
  • It would be good to see the code you have used to generate these timestamps. Commented Oct 2, 2012 at 15:10
  • Is the MySQL server on the same server? May be a timezone and/or difference in time between two machines causing this. Commented Oct 2, 2012 at 15:12
  • Have you tried just comparing time() with strtotime($dbobj->till_date) ? Still wouldn't cater for timezone differences between PHP and MySQL though. Commented Oct 2, 2012 at 15:20

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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`

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.