How do calculate the difference between 2 date time

Discussion in 'PHP' started by 8707, Feb 1, 2010.

  1. #1
    Hi all,

    I've 2 strings that representing the start and end values of a date and time.For example, start date = 30-01-2010 11:50 and end date = 02-02-2010 12:30. How can i calculate the difference between this 2 string?

    I want to calculate the difference in (day, hours and minutes) or just (hours and minutes). I'm a newbie for PHP so at this moment i've no idea that how to calculate the difference between two date and time.

    The date and time was save in text type which i capture it using $focus->column_fields['XXX'] = date('d-m-Y h:i A');
    Therefore, anyone can kindly provide me a sample coding to calculate the difference between date and time and return the result in text.(hours and minutes or maybe include days)
    Thank!
     
    8707, Feb 1, 2010 IP
  2. swarg

    swarg Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    swarg, Feb 1, 2010 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $start_date = '30-01-2010 11:50';
    $end_date = '02-02-2010 12:30';
    $seconds = strtotime($end_date) - strtotime($start_date);
    $days = floor($seconds / 86400);
    $seconds -= $days * 86400;
    $hours = floor($seconds / 3600);
    $seconds -= $hours * 3600;
    $minutes = floor($seconds / 60);
    $seconds -= $minutes * 60;
    echo "The difference is {$days} days, {$hours} hours, {$minutes} minutes, and {$seconds} seconds.";
    PHP:
     
    SmallPotatoes, Feb 1, 2010 IP
  4. 8707

    8707 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank for the reply!

    The $start_date and $end_date i will retrieve from database.
    For example $end_date is $focus->column_fields['cf_629'] and $start_date is $focus->column_fields['cf_628']. The result will return to $focus->column_fields['cf_630'].

    $focus->column_fields['cf_628'] = date('d-m-Y h:i A');
    $focus->column_fields['cf_629'] = date('d-m-Y h:i A');

    The date time will be capture in text type. For example 02-02-2010 03:00 PM.
    Could someone kindly provide the sample code for me! TQ
     
    8707, Feb 1, 2010 IP
  5. anxggxun

    anxggxun Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i think if you retrieve it from the database, you can calculate it on the database query, there's a function called TIMEDIFF() in mysql

    reference :
    http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff"]http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff

    example

    SELECT TIMEDIFF(end_date, start_date) as timediff
    FROM a_table
    Code (markup):
     
    anxggxun, Feb 2, 2010 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The code I gave you will work fine. Just replace $start_date with $focus->column_fields['cf_628']
     
    SmallPotatoes, Feb 2, 2010 IP
  7. 8707

    8707 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I try this but no success!

    if ($focus->column_fields['cf_628'] !="" && $focus->column_fields['cf_629'] !="")
    {
    	$q = "select timediff(cf_629, cf_628) as timediff from vtiger_ticketcf";
    	mysql_query($q);
    	$focus->column_fields['cf_481'] = mysql_fetch_object($q);
    	
    }
    Code (markup):
    Any idea how to fix it? tq
     
    8707, Feb 2, 2010 IP
  8. 8707

    8707 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I try to run this code but it return :
    The difference is 0 days, 0 hours, 0 minutes, and 0 seconds.
     
    8707, Feb 2, 2010 IP
  9. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That exact code?

    I just ran it here and got:

    The difference is 3 days, 0 hours, 40 minutes, and 0 seconds.
    Code (markup):
    If you mean that exact code, then you mis-pasted it.

    If you mean you tried it using the values from your database, then make sure you are also printing out the raw date values retrieved from the database so you can confirm they are correct.
     
    SmallPotatoes, Feb 2, 2010 IP
  10. 8707

    8707 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I've copy and pasted again but still no get the desire output !
     
    8707, Feb 2, 2010 IP
  11. 8707

    8707 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The problem it the date format. If i change to yyyy-mm-dd than i able to get the result. Thank for the help bro!
     
    8707, Feb 2, 2010 IP
  12. 8707

    8707 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thank for the guidance from you all and i think my problem is about to solve. I'm using the sample code provide by SmallPotatoes to solve my problem and the date format is yyyy-mm-dd hh:mm or date('Y-m-d H:i'), if i change to dd-mm-yyyy, it will not get the correct result so i want to know that is it possible to change the date format to dd-mm-yyyy if using strtotime()?
     
    8707, Feb 2, 2010 IP
  13. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #13
    Have you tried?
     
    danx10, Feb 8, 2010 IP