I'm trying to get the difference in time between two given times e.g. first time '17:00:01' and then second time '17:47:25' and here's what I tried so far,
$start_time = "17:00:01";
$end_time = "17:47:25";
echo $start_time->diff($end_time);
But seem's unfortunately not working, any help, ideas please?
My expected output must be like, if no difference in hours but there's difference in minutes and seconds then, "22 mins and 15 secs", If no difference in minutes but have difference in hours then, "2 hrs and 10 secs" but if only seconds in difference then, "22 secs".
Convert string time to timestamp and subtract. Convert timestamp to desired time format.
Just another way of doing it using string functions:
String in php is not object, so can't call
diff
method on it.Use
diff
method, you must change the string toDateTime
object.If you want to compare time difference in same day, you could try:
Here is it.
PHP
Output:
Now you can add some condition and make the real format.
Get difference between two times