Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Location
    pune
    Posts
    16

    Arrow Unanswered: PHP DateTime Calculation

    Hello Friends..

    I want to calculate time difference (in H:i:s format )between two times in php...i tried like this

    $t1= 08:32:46;
    $t2=08:34:56;

    $t1=strtotime(08:32:46);
    $t2=strtotime(08:34:56);

    $t3 =date("H:i:s",$t2-$t1);
    I got answer something like this ... 05:31:57

    and after that i tried using date with time like this

    $t1= 2012 - 02-08 08:32:46;
    $t2=2012 - 02-08 08:34:56;

    $t3 =date("H:i:s",$t2-$t1);
    I got answer something like this ...1970-01-01 05:31:57 ...in unixtimestamp its giving i think..

    Pleaase tel me how to calculate timings .

  2. #2
    Join Date
    Apr 2005
    Posts
    29
    I would use the 'Mysql Time Difference Function'. For example using TimeDiff('Year1 Time1', 'Year2 Time2') This query method returns the time difference between any two consecutive dates.

    TimeDiff('Year1 Time1', 'Year2 Time2')
    Code:
    mysql> SELECT TIMEDIFF('2009:01:08 00:00:00', '2008:01:01 00:00:00.000001');
    +---------------------------------------------------------------+
    | TIMEDIFF('2009:01:08 00:00:00', '2008:01:01 00:00:00.000001') |
    +---------------------------------------------------------------+
    | 8951:59:59.999999 |
    +---------------------------------------------------------------+
    1 row in set (0.00 sec)
    Web Database Software - Build your own php mysql database.

  3. #3
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    Or do it the long way.
    Convert hh:mm:ss to seconds, subtract start time from end time, and convert back to hh:mm:ss
    Code:
    diff=(end_hh*3600+end_mm*60+end_ss) - (start_hh *3600 + start_mm*60+start_ss)
    diff_hh=int(diff/3600)
    diff_mm=int(mod(diff,3600)/60)
    diff_ss=mod(diff,60)
    Assuming everything happens on the same day, and you are using a 24 hour clock.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    if you are wanting to do this inside PHP
    ..and assuming that you have to valid time values then use the PHP date time functions
    PHP: Date/Time Functions - Manual

    such as datediff

    which returns an interval object from which you can derive the elapsed whatever (years,months, hours, minutes, seconds whatever)

    if you cannot constrain your values then explcitly convert them to dates using strtotime
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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