Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting Duration (time) in PHP

Tags:

php

datetime

Outputting from a directions api I have a duration it will take the user to get from a to b. At the moment it is minutes but if the users journey will take 3 hours and 20 minutes it will output 200 minutes.

I would like it to work out that that is greater than 60 minutes. then divide by 60 and add the remainder to give

3 hours 20 minutes.

How do we do this.

Marvellous

like image 446
RIK Avatar asked Jun 30 '11 12:06

RIK


People also ask

How do I change from 24 hour format to 12 hour format in PHP?

Similarly, H will give you the hour in 24-hour format with leading zeros, but h will give you the hour in 12-hour format with leading zeros.

What is the format of DateTime in PHP?

$date=date_create("2013-03-15"); echo date_format($date,"Y/m/d H:i:s");

How can I get plus date in PHP?

The date_add() function adds some days, months, years, hours, minutes, and seconds to a date.


1 Answers

In case you have duration in seconds, you can format it with PHP gmdate() function:

echo gmdate("H:i:s", $seconds);

(Note: works for durations up to 24 hours)

like image 69
Dima L. Avatar answered Sep 24 '22 06:09

Dima L.