SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    time difference help

    I have the following

    PHP Code:
    $due substr($result['Due'], 05);
    $now date('h:i'); 
    due time is 12hr clock so 01:15

    how can i get to show how many mins between the two values

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,408
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    If it's a 12hr clock, how do you know if it's AM or PM ?
    And what does $result['Due'] contain, a complete timestamp?

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    On a very basic level

    Code:
    $d = (strtotime($now) - strtotime($due))/60;
    but I haven't tested whether that will work if $now is earlier than $due, and you'd want to check validity and so on. Though abs() would probably cover the first point. Also a quick google suggests you should look at the DateTime class to do stuff like this now.

    ETA: also what Guido said, result is probably meaningless if you don't know whether due time is am or pm. If you're truncating the due time because it also contains 'am' or 'pm', I get the impression strtotime() will handle that.
    http://www.firenza.net - my homage to a car from the 1970s

  4. #4
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    If it's a 12hr clock, how do you know if it's AM or PM ?
    And what does $result['Due'] contain, a complete timestamp?
    due contains 12hr time with seconds but i have to strip the seconds off.

    i now see why you ask about am or pm but none of the times have am or pm

  5. #5
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by droopsnoot View Post
    On a very basic level

    Code:
    $d = (strtotime($now) - strtotime($due))/60;
    but I haven't tested whether that will work if $now is earlier than $due, and you'd want to check validity and so on. Though abs() would probably cover the first point. Also a quick google suggests you should look at the DateTime class to do stuff like this now.

    ETA: also what Guido said, result is probably meaningless if you don't know whether due time is am or pm. If you're truncating the due time because it also contains 'am' or 'pm', I get the impression strtotime() will handle that.

    that actually works but it does show -5 tho

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    281
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, that must mean that the due time is later than the current time when you ran it. Change it to

    Code:
    $d = abs((strtotime($now)-strtotime($due))/60);
    But it won't make any sense if you don't know whether $due means 01:15 as 1:15am or 1:15pm.
    http://www.firenza.net - my homage to a car from the 1970s


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •