Difference between two datestamps

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Difference between two datestamps

    I have a date being returned from a mysql database like so
    2009-12-29 13:59:23
    I'm trying to check to see how many hours and minutes the current time has progressed since the datetime stamp was updated.

    So something like:
    Code:
    print date("Y-m-d G:H:s") - '2009-12-29 13:59:23';
    This always returns zero, any help much appreciated.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    date() returns a string so don't expect php to perform reliable arithmetic

    This problems strikes me as better handled in MySQL using NOW() and TIMEDIFF()
    Code:
    SELECT TIMEDIFF(NOW() - `mydatefield`)
    which I think returns the result in HH:MM:SS.ssss.
    If it must be php then you will need timestamp arithmetic
    Code:
    time()-strtotime('my date value')
    returning result in seconds

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Got what I wanted working using:
      Code:
      round((strtotime(date("Y-m-d G:H:s")) - strtotime($row['date']))/60 / 60)
      Gives me a rough time line to work off which will do for what I need it. Thanks for your help.

      Comment

      Working...