3 Replies - 1296 Views - Last Post: 16 March 2013 - 01:41 PM Rate Topic: -----

#1 matn89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-February 13

Compare two timestamps

Posted 16 March 2013 - 12:46 PM

Hi all.



I am grabbing two time stamps from a mysql db and storing them in variables, the timestamps are stored in the format YY-MM-DD HH:MM:SS .



Could anyone tell me how I might go about comparing two timestamps that are in this format, I need to check if one is older than the other.



Thank you for your time.
Is This A Good Question/Topic? 0
  • +

Replies To: Compare two timestamps

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,012
  • Joined: 12-December 12

Re: Compare two timestamps

Posted 16 March 2013 - 01:33 PM

It depends: what are the field data-types? Are they stored as strings or DATETIME (I assume) or TIMESTAMP?
Was This Post Helpful? 0
  • +
  • -

#3 matn89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 12-February 13

Re: Compare two timestamps

Posted 16 March 2013 - 01:35 PM

View Postandrewsw, on 16 March 2013 - 01:33 PM, said:

It depends: what are the field data-types? Are they stored as strings or DATETIME (I assume) or TIMESTAMP?


They are stored as the type TIMESTAMP .

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3517
  • View blog
  • Posts: 12,012
  • Joined: 12-December 12

Re: Compare two timestamps

Posted 16 March 2013 - 01:41 PM

If they are DATETIME (or TIMESTAMP) fields then it is more efficient to only retrieve the dated-records that you need, in the SQL statement:

WHERE date1 < date2


..but if you need them as dates in the PHP then you might use strtotime() to convert them, having retrieved the values from the database. discussed here

Of course, this being PHP :), there are a dozen other ways to approach this.

This post has been edited by andrewsw: 16 March 2013 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1