Results 1 to 5 of 5

Thread: date() function

  1. #1

    date() function

    Hey guys, I know you guys are pretty smart around here, so I thought I'd ask here.

    I'm trying to get a PHP script to count the number of hours between two time stamps. This is what the code looks like:

    PHP Code:
    $startdate $_POST['datetime'];
    $enddate date("Y-m-d H:i:s");
    $starttime $_POST['time'];
    $endtime date("H:i:s");
    $totaltime $endtime $starttime;
    $totaldate $enddate $startdate;
    echo 
    $totaltime."<BR>".$totaldate
    Does anyone know how to make this display the information correctly? The POST data is formatted the exact same way as the data on this page ("Y-m-d H:i").
    fraudservers.com - for sale!

  2. #2
    Join Date
    Jul 2001
    Location
    Glasgow, Scotland
    Posts
    129
    You should use UNIX timestamps when calculating difference in dates, makes it a lot easier for you

    See http://www.php.net/manual/en/function.time.php for more detail.
    My development blog - (un)Interesting codesnippets and the occational code-related rant!

  3. #3
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    PHP Code:
    $starttime strtotime($_POST['datetime']." ".$_POST['time']);
    $endtime time();
    $totaltime $endtime $starttime
    What you get is the number of seconds between the two times. Displaying it nicely is left as an exercise to the reader (it's pretty easy, eg. (int)($totaltime / 3600) is the number of hours, then % 3600 is what's left over to be divided by 60, etc.).

  4. #4
    The problem with that is, and I didn't make this evident before, is that I need to store the two previous dates in a SQL DB.
    fraudservers.com - for sale!

  5. #5
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082
    So what's the problem? There are a couple good ways you could store any of that information in the DB. Either store it in an integer field as a timestamp, or as one of the DATETIME datatypes in your database. At least in Postgres, you can subtract them directly in the DB to get a difference. Or you can just get the timestamp value of the datetime field from the DB, and do the math in PHP. I don't see what the problem is..

Posting Permissions

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