Reputation: 5846
I have a string in the format of hh:mm:ss that I convert to an integer representing the total number of seconds. For example:
01:43:03
01 * 3600
43 * 60
03 * 1
The above example results in an integer value of 6183.
After performing some logic using this value I then need to convert the integer back to a strict format of hh:mm:ss.
Note that this isn't a time of day. It is an amount of time. Like, I spent 1 hour, 43 minutes and 3 seconds driving from one point to another.
What is the shortest way of coding this conversion so that 6183 seconds is formatted as 01:43:03 ?
Upvotes: 2
Views: 5298
Reputation: 34914
You can simply use like this
<?php
$total_seconds = 160183;
$seconds = intval($total_seconds%60);
$total_minutes = intval($total_seconds/60);
$minutes = $total_minutes%60;
$hours = intval($total_minutes/60);
echo "$hours:$minutes:$seconds";
?>
Check live demo : http://sandbox.onlinephpfunctions.com/code/78772e1462879ce3a20548a3a780df5de4e16e2c
Upvotes: 1
Reputation: 187
Use this-
echo $time = date("h:i:s A T",'6183');
I think it will help.
Upvotes: 2