0
$time1 = new DateTime($post->clock_in);
$time2 = new DateTime($post->clock_out);
$interval = date_diff($time1,$time2);
$difference = $interval->format('%H:%I');

In this above code I want to get exact time between two different time. Now, What happen if $time1 = '10:00' and $time2 = '05:00' then difference between these two time is 07:00 but it show 05:00. I don't know why? So, How can I get the exact timing between two timing? Please help me.

Thank You

Klint
  • 114
  • 2
  • 10

1 Answers1

0

use

$d1=new DateTime("2012-07-08 11:14:15.638276");
$d2=new DateTime("2012-07-08 11:14:15.889342");
$diff=$d2->diff($d1);
print_r( $diff ) ;

https://www.php.net/manual/en/class.dateinterval.php

Sergey
  • 111
  • 1
  • 7
  • How does that differ? Using `10:00` and `05:00` will also return a 5 hour difference – brombeer Aug 13 '20 at 11:14
  • it's right - difference 5 hours, use correct format of time) see http://sandbox.onlinephpfunctions.com/code/4b5492e063c3d487b8be82b00a0708b9ffda5b57 – Sergey Aug 13 '20 at 11:22