How can I convert
00:00:46.70
to
T0M46S
I try
$date = new DateTime($time); echo $date->format('\P\TG\Hi\M');
but it gives me something like this:
PT0H00M
Note I want duration... not date!
How can I convert
00:00:46.70
to
T0M46S
I try
$date = new DateTime($time); echo $date->format('\P\TG\Hi\M');
but it gives me something like this:
PT0H00M
Note I want duration... not date!
There is no native function, but you can do it with native object DateTime : calculate the duration from midnight to your time.
<?php
$time = new DateTime('00:00:46.70');
$midnight = new DateTime('00:00:00.00');
$period = $midnight->diff($time);
echo $period->format('T%iM%SS'); //output T0M46S
print_r($period);
Here is a php sandbox to test it
Carbon has the solution
function secondsToISO8601Format(int $seconds): string
{
return Carbon\CarbonInterval::seconds($seconds)->cascade()->spec();
}