3

I have 2 dates from mysql with datetime format.

Date 1 is:

"2012/09/28 09:28:00" (column name is departure_date)

and Date 2 is:

"2012/09/29 10:48:00" (column name is return_date)

So how to get a result like this: "25 hours 20 minutes"

I don't care about the seconds.

I'm using MySQL and PHP for the programming language.

4 Answers 4

6

Date difference function already exists

SELECT DATEDIFF('2007-10-04 11:26:00','2007-10-04 11:50:00')

Also you can try TimeDiff

SELECT TIMEDIFF('2007-10-05 12:10:18','2007-10-05 16:14:59') AS duration;

So in your case you have the option to use these two functions like this

SELECT TIMEDIFF(departure_date,return_date) AS duration;
SELECT DATEDIFF(departure_date,return_date) AS duration;
Sign up to request clarification or add additional context in comments.

Comments

3

You could use DateInterval class.

$date1 = new DateTime("2012/09/28 09:28:00");
$date2 = new DateTime("2012/09/29 10:48:00");

$diff = $date1->diff($date2);
echo $diff->format('%d days %h hours %i minutes');

Check here for the format.

Comments

2

You can do it example by this:

$date1 = "2012/09/28 09:28:00";
$date2 = "2012/09/29 10:48:00";

$difference = abs(strtotime($date2)-strtotime($date1));

$hours = floor($difference / (60*60));
$minutes = floor(($difference - $hours * 60*60) / 60);

echo "{$hours} hours {$minutes} minutes";

And it will give you exactly what are you looking for:

25 hours 20 minutes

Comments

1

try this:

select concat(substr(mt,1,locate('.',mt)-1),' hours') hours,
       concat(ceil((substr(mt,locate('.',mt)+1) * .60)/100 ),' minutes') 
                                                                     as minutes
from (
select TIMESTAMPDIFF(MINUTE,
                      '2012/09/28 09:28:00','2012/09/29 10:48:00')/60 as mt)a


SQL Fiddle demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.