Date Difference

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

  1. #1
    I have two strings in datetime format ie yyyy-mm-dd hrs:mins:secs
    i want to calculate number of hours between the two
    also even a comparison if two string are equal or not would do
    help needed
     
    mandarj123, Mar 28, 2009 IP
  2. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use strtotime() function to convert dates into unix timestamp. For example:

    
    // Convert date to timestamp
    $timestamp1 = strtotime('2009-03-28 12:00:00');
    $timestamp2 = strtotime('2009-03-29 12:00:00');
    
    // Get difference in seconds
    $difference = $timestamp2 - $timestamp1;
    
    // Get number of hours
    $hours =  $difference / 3600; // 3600 is number of seconds in one hour
    
    echo $hours;
    
    PHP:
     
    steelaz, Mar 28, 2009 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    As steelaz said, strtotime convert datetime to timestamp , which is in seconds & can be manage easily.

    - ads2help
     
    ads2help, Mar 28, 2009 IP
  4. Stylesofts

    Stylesofts Peon

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

    u can do like this as welll

    $date1 = mktime($hr,$min,$sec,$fromMonth,$fromDay,$fromYear);

    $date2 = mktime($Tohr,$Tomin,$Tosec,$ToMonth,$ToDay,$ToYear);
    $dateDiff = $date2 - $date1;
    $fullDays = floor($dateDiff/(60*60*24));


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