Time difference in hour between two dates?

Discussion in 'PHP' started by aronsthlm, Dec 17, 2009.

  1. #1
    Hey guys,

    I want to compare the time difference, or hours really (as in hour count) between two dates.

    I store the dates in mysql in a separate date/time row which has the following format:

    2009-10-29 13:00:00

    And, what I want to do is calculate and print how many hours there are between, let's say that date, and current time.

    Have absolutely no idea how to do this but hopefully it's not too hard.
     
    aronsthlm, Dec 17, 2009 IP
  2. NodLemon

    NodLemon Peon

    Messages:
    27
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use mktime to convert your time from the database ( string ) to the UNIX timestamp and compare it with time ( returns the current time - UNIX timestamp format ).
     
    NodLemon, Dec 17, 2009 IP
  3. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Right - just to further explain, here's a bit of code. I personally use strtotime, which converts time into a UNIX datestamp too, just with a bit more flexibility, (UNIX datestamp being the number of seconds since January 1 1970 00:00:00 UTC, makes sense to geeks, don't worry!) and then it's easy to do stuff:

    $firstTime = [your first time here];
    $timeNow = strtotime('now'); // get current time - it knows what "now" is, or you can do "next Friday" and stuff like that, that's why it's more flexible than mktime
    $timeDiff = ($timeNow - $firstTime) / 3600; // divide by 3600 to get hours
    Code (markup):
    Easy-peasy, though dates and times can seem scary, and I think they used to be, now they can easily be handled in UNIX format and then converted back to whatever format you want using the php date() function. I used this code to do exactly what you want in a script of mine that only runs every 24 hours, but is called every ten minutes and checks to see if 24 hours have passed yet since the last run.
     
    Last edited: Dec 17, 2009
    markowe, Dec 17, 2009 IP
  4. erandika1986

    erandika1986 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is so easy. You have to create PHP function for that and pass the datetime parameters to the function and get the time difference in hours.

    <?php

    function timeDiff($firstTime,$lastTime)
    {

    // convert to unix timestamps
    $firstTime=strtotime($firstTime);
    $lastTime=strtotime($lastTime);

    // perform subtraction to get the difference (in seconds) between times
    $timeDiff=$lastTime-$firstTime;

    // return the difference
    return $timeDiff;
    }

    //Usage :
    echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");

    ?>
     
    erandika1986, Dec 17, 2009 IP
  5. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #5
    Oh, ahem, of course, I would write it as a function too :)
     
    markowe, Dec 17, 2009 IP
  6. leeann.william

    leeann.william Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hiii,
    you can do this by extracting the string like. month, date, year, from the value that is entered in the database. After that you can we able to compare to date.

    or you can do this by using this method.

    echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");


    Try this. If it don't work give me a reply message. i will send you all the coding about how to do that.
     
    leeann.william, Dec 17, 2009 IP