1

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);
user229044
  • 232,980
  • 40
  • 330
  • 338
ToddN
  • 2,901
  • 14
  • 56
  • 96
  • Check this out, this should give you what you need: http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php – Matt Dodge Feb 20 '12 at 21:57

1 Answers1

2

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);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • 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 08:10