0

I have two timestamp like below format in my database.

$time1 = 4/19/2019 12:21:01 AM
$time2 = 4/19/2019 12:22:50 AM

I want get difference between this two timestamp like

12:21:01

Let me know if someone can give me idea/solution for do it using php.

Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
Raju Bhatt
  • 385
  • 5
  • 17
  • there is no *one* difference. the second value has no date, so your difference could be any multiple of one day. unless you *assume* a date, what you want to achieve has no actual value. *if* you assume a date, just convert it to DateTime-objects and substract them. – Franz Gleichmann Apr 25 '19 at 18:00
  • I think You understood it wrong, Let me edit my question. Thanks – Raju Bhatt Apr 25 '19 at 18:02
  • @FranzGleichmann he has added the sample of timestamp and then he needs the output like the other. – Asif Apr 25 '19 at 18:03
  • I have edited my question for make it more clear. Thanks – Raju Bhatt Apr 25 '19 at 18:03

2 Answers2

2

You can get the difference as

$time1 = '4/19/2019 12:21:01 AM';
$time2 = '4/19/2019 8:15:01 PM';

$start    = new DateTime($time1); 
$end      = new DateTime($time2); 
$diff     = $start->diff($end); 
print $diff->format("%H:%I:%S");

For more details PHP Manual

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
2

So you should have mysql select query like this:

Select time1,time2,time_format(abs(timediff(time1,time2)), "%H:%i") as diff From table1

You can change time format, follow this link for time_format function

Asif
  • 350
  • 2
  • 10