I currently get duration in this format: 04:00
but I need in this format 00:04:00
.
I am using this code but it's not working correctly.
$time = date("h:i:s", strtotime($duration));
echo $time;
try like this,
$duration = "00:"."04:00";
The problem is the strtotime($duration)
; It doesn't know exactly what format that's in, and can't get a valid date from it. (This is why you're getting unexpected results from the date
function.
If you want to just simply add 00:
before $duration
, you can do it like so:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime
function, and would likely get better results.
This should work for you:
(Your problem is in which format you read in your time, so DateTime::createFromFormat
)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output:
00:04:00