Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert H:i to ISO 8601 duration standard

Tags:

php

datetime

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!

like image 548
Manos Krokos Avatar asked Mar 22 '23 06:03

Manos Krokos


1 Answers

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');
like image 102
Glavić Avatar answered Apr 21 '23 13:04

Glavić