2

I have two time values as follows

$start = '13:00:00';
$end = '21:00:00';

I want calculate the difference between these two time values, I want difference value in format like 00:00:00 (here result would be 08:00:00)

I am using below code to calculate difference

$time = date( "h:i:s", strtotime($end) - strtotime($start));

but it gives me result as 01:30:00

please help if anyone have any idea, i do not want to use DateTime class

In my opinion it is not duplicate, if it is tell me the answer if your can find in duplicate question

Manoj
  • 477
  • 5
  • 8
  • 19

3 Answers3

2
   $datetime1 = new DateTime('13:00:00');
   $datetime2 = new DateTime('21:00:00');
   $interval = $datetime1->diff($datetime2);
   echo $interval->format('%H:%I:%S');
Anthony
  • 36,459
  • 25
  • 97
  • 163
  • 1
    The OP states he does not want to use DateTime class. – Dincho Todorov Aug 29 '14 at 12:22
  • Is there any way to calculate this without using DateTime class? – Manoj Aug 29 '14 at 12:22
  • The op said he didn't want to deal with dates. This doesn't force using dates – Anthony Aug 29 '14 at 12:23
  • the only issue with `DateTime` is i'm getting this error `Fatal error: Call to undefined method DateTime::diff()` may be because i might not have some libraries – Manoj Aug 29 '14 at 12:25
  • And no, there isn't a way to do the above without using the class. Maybe thter are orher ways to get your value, but there isn't a secret other class that works just like DateTime class but has a name you like more – Anthony Aug 29 '14 at 12:25
  • It means your version of php is out of date. 5.2 is no longer supported. – Anthony Aug 29 '14 at 12:27
  • @Anthony actually there is very simple way, using just simple math, see the duplicate – Dincho Todorov Aug 29 '14 at 12:37
  • Im sure there is. I meant there isn't a way to use my suggestion without the class. – Anthony Aug 29 '14 at 12:41
1

Try with this code :

$time1 = strtotime('13:00:00');
$time2 = strtotime('21:00:00');
$diff = $time2 - $time1;
echo 'Difference: '.date('H:i:s', $diff);
Jenz
  • 8,280
  • 7
  • 44
  • 77
0

Its easier to do that with the DateTime::diff function.

There is an example an that site.

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
Anthony
  • 36,459
  • 25
  • 97
  • 163
René Höhle
  • 26,716
  • 22
  • 73
  • 82