Home » Php » php – How can I make timediff function to convert negative time to positive?

php – How can I make timediff function to convert negative time to positive?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have query where I have to find the difference between two times , it works well when the first time is greater than the second time .

But when the first one is smaller than the second one it returns a negative time; Just wondering is there any way to make it positive?

Below is the query that returns negative time.

SELECT TIMEDIFF("13:10:11", "13:20:10");

output

-00:09:59

expected output

00:09:59
How to&Answers:

If what you want is to get the absolute time difference between two times, you could use a CASE expression to swap the times so that the first was always greater than the second e.g.

SELECT CASE WHEN time1 < time2 THEN TIMEDIFF(time2, time1)
            ELSE TIMEDIFF(time1, time2)
       END AS delta

Answer:

Use DateDiff with MINUTE difference:

 SELECT DATEDIFF(MINUTE, '11:10:10' , '11:20:00') AS MinuteDiff