Manos Krokos
Manos Krokos

Reputation: 113

Convert H:i to ISO 8601 duration standard

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!

Upvotes: 4

Views: 1991

Answers (1)

Glavić
Glavić

Reputation: 43552

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');

Upvotes: 7

Related Questions