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!
$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:
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
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):
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
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.
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!
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()?