0

I have 2 times (DateInterval - php) and I would like to get the difference between them (something like "diff()" function for DateTime).

My situation:

$task->estimate->format('%H:%I');          // HOURS:MINUTES / DateInterval 
$task->total_time->format('%H:%I');        // HOURS:MINUTES / DateInterval 
$task->total_time->diff($task->estimate);  // Not workin, only for DateTime
// difference between them?

It would be great without counting it in second, there must be easy way to do this.

Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • 5
    Possible duplicate of [PHP difference between two times](http://stackoverflow.com/questions/34486600/php-difference-between-two-times) – niceman Mar 19 '17 at 15:46
  • What is the question? – Jared Farrish Mar 19 '17 at 15:48
  • @niceman Nope, date_diff doesn't work (date_diff() expects parameter 1 to be DateTimeInterface, object given) and date_create() doesnt works too (not accepting normal nor formated dates of type DateInterval) – Jax-p Mar 19 '17 at 15:50
  • @JaredFarrish how to get difference between two values of type "DataInterval" (NOT DateTime) – Jax-p Mar 19 '17 at 15:51
  • diff of (now+interval1) and (now+interval2) – splash58 Mar 19 '17 at 15:55
  • @splash58 how do you mean it? can you show php example? – Jax-p Mar 19 '17 at 16:00
  • 1
    If you can make your estimate and total time into total number of minutes (or seconds...), you can subtract the two total number of seconds and then turn that into a number of minutes and/or hours display. – Jared Farrish Mar 19 '17 at 16:05
  • Possible duplicate of [How to get time difference in minutes in PHP](https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – localheinz Apr 30 '18 at 13:23

1 Answers1

2

You can calculate diif of (any time + interval1) and (the same time + interval2)

$estimate = new DateInterval('PT6H8M');
$total_time  = new DateInterval('PT5H7M');
$time = new DateTime('midnight');
$time->add($total_time);
$time1 = new DateTime('midnight');
$diff = $time1->add($estimate)->diff($time);
var_dump($diff); // object(DateInterval) ... ["h"]=> int(1) ["i"]=> int(1)  ...
splash58
  • 26,043
  • 3
  • 22
  • 34