0

I am trying to find time differences between two times. This is the code I have:

$ddtcounter1="2014-03-06 20:20:30";

$ddtcounter2="2014-03-07 21:20:30";

$today = new DateTime($ddtcounter2);
$pastDate = $today->diff(new DateTime($ddtcounter1));

$total= $pastDate->h . ".";
$total2=$total.$pastDate->i;

echo $total2; //echo the time difference in hour.minutes form (Exmple:2.1)

How can I find the time differences? I have read few guides and tutorials and they didn't help. I read a lot of answers but they didn't work for me...

EDIT: Example (For who didn't understand the question):

First time: 2014-03-06 20:30:30

second is: 2014-03-07 19:30:30

The time difference will be 23.0 (23 hours), because it happened 1 day earlier!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
morha13
  • 1,847
  • 3
  • 21
  • 42

1 Answers1

0

This should work:

$ddtcounter1="2014-03-06 20:15:30";
$ddtcounter2="2014-03-07 21:20:30";

$date1 = new DateTime($ddtcounter1);
$date2 = new DateTime($ddtcounter2);
$diff = $date1->diff($date2);

$hours = $diff->d * 24;
$hours += $diff->h;
echo $hours;
echo $diff->i; // Minutes
echo sprintf('%s.%s', $hours, $diff->i); // Hours.Minutes
  • This is not the problem... The problem is that it shows the different between the times only. in this exmple its say 1 hour different, its not check the timediff between the dates... – morha13 Mar 07 '14 at 18:42
  • 1
    I, totally, do not, follow, what you mean. This shows `hours` and `minutes`. What do you want to achieve? –  Mar 07 '14 at 18:46
  • I want that its will show the date different in hours and too... If the first one is: 2014-02-06 20:30:30 and the second is: 2014-02-07 19:30:30 the time difference will be 23.0 (23 hours) – morha13 Mar 07 '14 at 18:48
  • You mean that you want to subtract a piece of the date from each other? I'm sorry, but I do not follow what you mean. –  Mar 07 '14 at 18:49
  • ok, its almost working! I just need to get the minutes! Thank you! – morha13 Mar 07 '14 at 19:12
  • Updated. Try again :P –  Mar 07 '14 at 19:16