4

The duration time for a recipe is stored in my database as a string like this: "01:50" I am trying to convert it to the ISO 8601 duration standard with no success.

This is what I use to call the field and parse it the page:

$totalTime = ($extraField->value);
$itempr = "itemprop=\"totalTime\" content=\"$totalTime\"";

Like this, it appears in the Google structured data testing tool as "00:50" which is not accepted by Google. From what I understand, it should appear like this: PT1H50M

Has anyone got any ideas on how to convert it to the ISO 8601 format? Please, keep in mind that I am not to keen on development with PHP so be as explanatory as possible. Thanks!

Manos Krokos
  • 113
  • 1
  • 3
  • 12
  • 2
    Possible duplicate: http://stackoverflow.com/questions/5322285/how-do-i-convert-datetime-to-iso-8601-in-php?rq=1 – Amal Murali Sep 29 '13 at 18:27
  • So, `PT1H50M` is what you want? [You might want to look at this](http://www.php.net/manual/en/datetime.format.php), or possibly [this one](http://www.php.net/manual/en/dateinterval.format.php). – Wrikken Sep 29 '13 at 18:33
  • 1
    OK, I got it! $datetime = new DateTime($extraField->value); $totalTime = $datetime->format('\P\TG\Hi\M'); $itempr = "itemprop=\"totalTime\" content=\"$totalTime\""; – Manos Krokos Sep 29 '13 at 18:44
  • possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – vascowhite Sep 30 '13 at 14:32

1 Answers1

7

Like you already wrote in the comments :

echo (new DateTime('01:50'))->format('\P\TG\Hi\M');

or you can convert it already in the database using TIME_FORMAT function :

SELECT TIME_FORMAT('01:50', 'PT%kH%iM');
Glavić
  • 42,781
  • 13
  • 77
  • 107