1

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!

frmbelz
  • 1,537
  • 16
  • 22
partiz
  • 1,196
  • 2
  • 12
  • 33
  • possible duplicate of [PHP: How to convert string duration to ISO 8601 duration format? (ie. "30 minutes" to "PT30M")](http://stackoverflow.com/questions/13301142/php-how-to-convert-string-duration-to-iso-8601-duration-format-ie-30-minute) – Thilo Jun 05 '15 at 00:19
  • @Thilo I should can do it with native php function...no with a custom function...I saw that post before – partiz Jun 05 '15 at 00:21
  • You can leave a comment on the other post, asking if there is such a native php function. – Thilo Jun 05 '15 at 00:23
  • Looking at another post (http://stackoverflow.com/a/19082328/14955) your code should work. Don't you just need to add the part about seconds in the format string? – Thilo Jun 05 '15 at 00:25

1 Answers1

0

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

Alexandre Tranchant
  • 4,074
  • 4
  • 35
  • 63