Timediff In php

Discussion in 'PHP' started by mandarj123, Mar 27, 2009.

  1. #1
    Hi

    How to get the timedifference between two datetime values one im fetching frm database and other im comparing from input i have both dates in form

    yyyy-mm-dd hr:min:sec

    i want to get timediff between two values in above format

    please help...
     
    mandarj123, Mar 27, 2009 IP
  2. kusal

    kusal Peon

    Messages:
    91
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use strtotime to convert two dates into seconds

    $dbdate_seconds = strtotime($dbdate);
    $inputdate_seconds = strtotime($inputdate);
    
    $time_diff = $dbdate_seconds - $inputdate_seconds;
    
    //you will get the time difference in seconds, divide by 60 to make it minutes
    Code (markup):
    or you can use the mysql TIMEDIFF

    $query = select TIMEDIFF(field_name, '$input_date') from tbl_name where condition 
    Code (markup):
     
    kusal, Mar 27, 2009 IP
  3. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    u can use like this
    $date1 = mktime($hr,$min,$sec,$fromMonth,$fromDay,$fromYear);

    $date2 = mktime($hr1,$min1,$sec1,$ToMonth,$ToDay,$ToYear);
    $dateDiff = $date2 - $date1;
    $timeDifference= floor($dateDiff/(60*60*24));



    Regards
    Stylesofts Developing Team
     
    Stylesofts, Mar 29, 2009 IP
  4. ryankirgan

    ryankirgan Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I do it like this:

    <?php
    $sttime =  microtime(true); //start timer for time it takes to load the page - put as first line on page
    ...
    $timediff = round((microtime(true) - $sttime), 5); //ends time - put as close to the end of the page as possible.
    ?>
    PHP:
    hth
     
    ryankirgan, Mar 29, 2009 IP