Question:

PHP/MySQL DATETIME problem

by  |  earlier

0 LIKES UnLike

I'm working on a PHP web application which connects to a MySQL database. This database has a table labels "events", the first column of which is a DATETIME field labeled "event_stamp".

Two examples entries would look like:

7/22/2008 4:53:42 PM

7/22/2008 5:03:24 PM

How would I go about finding the difference in these 2 times in the PHP environment?

 Tags:

   Report

2 ANSWERS


  1. The PHP strtotime function converts to seconds since the epoch (1970-01-01 00:00:00) and is capable of handling all kinds of formats.  The only problem with older versions (through PHP 4) is that dates before the epoch yield 0 instead of the correct negative number.


  2. Not sure because I haven't actually tested this.....

    But, I am pretty sure php is smart enough that you can just subtract the two to get the difference.

    =====edit========

    So I did some toying, along with some searching of the php manual (linked in sources... )

    making use of the mktime() function seems to be key.

    mktime() converts variables of the date() format to the unix timestamp (what the other guy is talking about...) and mktime() knows the difference between AM and PM.

    This gives you something to subtract from.  The unix timestamp has a unit of seconds.  So say you have:

    $date1 = mktime($times[$i]);

    $date2 = mktime($times[($i+1)]);

    $date3 = $date2 - $date1;

    echo $date3;

    your app might display something like "582"

    This means the difference is 582 seconds.

    You can then do some math to convert that to other format (hours:minutes:seconds, for example)

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.