Page 1 of 1
How do calculate the difference between 2 time and date
Posted: Sun Jan 31, 2010 10:12 pm
by 8707
Hi all,
I've 2 strings that representing the start and end values of a date and time.For example, start date = 2010-01-30 14:50 and end date = 2010-02-02 12:30. How can i calculate the difference between this 2 string?
TQ
Re: How do calculate the difference between 2 time and date
Posted: Sun Jan 31, 2010 10:20 pm
by AbraCadaver
8707 wrote:Hi all,
I've 2 strings that representing the start and end values of a date and time.For example, start date = 2010-01-30 14:50 and end date = 2010-02-02 12:30. How can i calculate the difference between this 2 string?
TQ
Depends upon what you mean by the difference. The number of days, months and years between the two? The number of seconds between the two? Or what?
Re: How do calculate the difference between 2 time and date
Posted: Mon Feb 01, 2010 12:12 am
by pbs
You can use mktime() function to calculate difference between 2 dates.
Re: How do calculate the difference between 2 time and date
Posted: Mon Feb 01, 2010 2:47 am
by Christopher
I would recommend using PHP's DateTime classes. See the manual.
Re: How do calculate the difference between 2 time and date
Posted: Mon Feb 01, 2010 10:44 am
by klevis miho
Use strtotime() function:
$difference = strtotime($end_date) - strtotime($start_date); //will return the difference in seconds
Re: How do calculate the difference between 2 time and date
Posted: Mon Feb 01, 2010 7:01 pm
by 8707
Hi
Thank for the reply. In fact, 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)
TQ.
I found this from another website :
Code: Select all
function dateDifference($date1, $date2)
{
$d1 = (is_string($date1) ? strtotime($date1) : $date1);
$d2 = (is_string($date2) ? strtotime($date2) : $date2);
$diff_secs = abs($d1 - $d2);
$base_year = min(date("Y", $d1), date("Y", $d2));
$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
return array
(
"years" => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
"months" => date("n", $diff) - 1,
"days_total" => floor($diff_secs / (3600 * 24)),
"days" => date("j", $diff) - 1,
"hours_total" => floor($diff_secs / 3600),
"hours" => date("G", $diff),
"minutes_total" => floor($diff_secs / 60),
"minutes" => (int) date("i", $diff),
"seconds_total" => $diff_secs,
"seconds" => (int) date("s", $diff)
);
}
Can i use this code directly or need to do some modification first?
Re: How do calculate the difference between 2 time and date
Posted: Mon Feb 01, 2010 9:45 pm
by Christopher
Re: How do calculate the difference between 2 time and date
Posted: Tue Feb 02, 2010 8:54 pm
by 8707
Thank for the guidance from you all and i think my problem is about to solve. I'm using strtotime() 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()?
Below is my solution :
Code: Select all
if ($focus->column_fields['xxx'] !="" && $focus->column_fields['yyy'] !="")
{
$start_date = $focus->column_fields['xxx'];
$end_date = $focus->column_fields['yyy'];
$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;
$focus->column_fields['cf_481'] ="{$days} days, {$hours} hours, {$minutes} minutes";
}