Take the 2-minute tour ×

Unfortunately I cannot use DateTime::Diff and am looking for a way to convert my current format of datetime so I may subtract and find the difference in hours and minutes, something like this:

// 2:11pm 2-20-2012
$opendate = '2012-02-20T14:11:03-05:00';

// 6:22pm 2-20-2012
$closedate = '2012-02-20T18:22:50-05:00';

$diff_date = $closedate - $opendate

I would like $diff_date to return in hours::minutes format, such as '4 hrs 11 min'.

If it would be beneficial for this, another alternative to the times formatted above is:

        $removeT = "T";
        $replaceSPACE = " ";
//returns datetime like '2012-02-20 14:11:03'
        $r = str_replace($removeT, $replaceSPACE, $opendate[$i]);
        $r = substr($r, 0, 19);
//returns datetime like '2012-02-20 18:22:50'
        $s = str_replace($removeT, $replaceSPACE, $closedate[$i]);
        $s = substr($s, 0, 19);
share|improve this question
Check this out, this should give you what you need: stackoverflow.com/questions/676824/… –  mattedgod Feb 20 '12 at 21:57
add comment

1 Answer

up vote 2 down vote accepted

Convert both to UNIX-timestamp, then subtract one from the other. You can convert to result to hours and minutes very easy.

$secondsDiff = strtotime($closedate) - strtotime($opendate);
share|improve this answer
Ok im very new to php, but from my understanding I would convert those time "values" to UNIX using mktime()? –  ToddN Feb 20 '12 at 21:58
This worked beautifully, this converts it into seconds. I'll do math to figure out hours and minutes thank you. –  ToddN Feb 20 '12 at 22:04
You use mktime(), when you have every time component (hour, minute, ...) on its own. strtotime() tries to parse a formatted time-string into a timestamp (quite useful in conjunction with date()). –  KingCrunch Feb 21 '12 at 8:10
add comment

Your Answer


