Jump to content

Converting minutes to itemprop duration (ISO 8601)


MilenKo
 Share

Recommended Posts

Hello.

Working on my cooking recipes profile I need some time conversion to preptime duration as per ISO 8601. For the moment I have a few lines of code that take the value of $page->recipe_cook_time (in minutes) and convert it to human readable time for the frontend (ex. 1d 2h 35m):

$d = floor ($page->recipe_cooking_time / 1440);
$h = floor (($page->recipe_cooking_time - $d * 1440) / 60);
$m = $page->recipe_cooking_time - ($d * 1440) - ($h * 60);
if ($d > 0) {
$cooking_time = $d . ' d ';
}
if ($h > 0) {
$cooking_time .= $h . 'h ';
}
if ($m > 0) {
$cooking_time .= $m . 'min';
}

Now I am trying to convert the minutes of $page->recipe_cook_time into ISO 8601 duration (ex. P1DT1H35M) but am stuck and get different results instead of the right timing. Any suggestions about the conversion or even if you know of a function that can smarter convert minutes to human readable D:H:M ?

Link to comment
Share on other sites

@LostKobrakai I checked the link you kindly provided, but this is to convert time in different formats but I do not see it doing it for the ISO standard.

@Cengiz Deniz Thanks for the suggestion. It loos shorted for sure, now the question would be to make it work promptly for the format of days, hours, minutes as some recipes might require longer than 23h 59 minutes to prepare and I need to make sure that if day or hour are empty, they do not show as 0d 0h 25m (for example).

But I will test your solution adding the day and see if that works or not.

Still I am missing the ISO formatting. I believe I can do the same approach as with the human time modifying the code but will test today and if working will share it back.

 

Link to comment
Share on other sites

@LostKobrakai Even though you said that DateInterval is stupid, I found it as one of the sollutions but gave up on it as it blows an error on new DateInterval when the time is in minutes but not already in ISO format.

I know I could either change the field to add the time as a text field and add there the time in ISO or else that would be easy to convert, however I am more eager to find out a more custom approach as in our life we could never know what to expect from tomorrow :)

Link to comment
Share on other sites

Ok guys, I guess being tired sometimes takes your concentration away. This morning I decided to use the same approach I used for the human readable time and the ISO 8601 duration is now fully working. As far as I can't think of a use for a period longer than X-days, I did not added the years format in human readable nor ISO but if someone needs it, just follow the logic and you will be good.

Here is the code that I used:

<?php 	$d = floor ($page->recipe_cooking_time / 1440);
		$h = floor (($page->recipe_cooking_time - $d * 1440) / 60);
		$m = $page->recipe_cooking_time - ($d * 1440) - ($h * 60);
		if ($d > 0) {
			$cooking_time = $d . 'd ';
			$itemprop = $d . 'D';
		}
		if ($h > 0) {
			$cooking_time .= $h . 'h ';
			$itemprop .= 'T' . $h . 'H';
		}
		if ($m > 0) {
			$cooking_time .= $m . 'm';
			$itemprop .= $m . 'M';
		}
		$itemprop = 'P' . $itemprop;
?>

If someone has a more elegant approach, please share it. My goal was to remove any 0 days, hours or minutes if the time field calls for less.

Link to comment
Share on other sites

  • 9 months later...

Hello again, folks. As my project is close to the end and I am on the stage of implementing schema.org markup, I decided to revisit my old post and share the code that suits my needs so far.

So as previously mentioned, I am having numeric fields holding the prep_time & cook_time interval in minutes. I decided to have that instead of going to a text field and entering the values manually (1h 25min) as to standardize and unify the appearance in case different people would be allowed to share content. So here is my final code that I used to convert from minutes to X d Yh Zmin ZZsec (original source provided for further reference and explanations ;) )

Spoiler

//Original Source: https://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds
function convert($seconds){
$string = "";

$days = intval(intval($seconds) / (3600*24));
$hours = (intval($seconds) / 3600) % 24;
$minutes = (intval($seconds) / 60) % 60;
$seconds = (intval($seconds)) % 60;

if($days> 0){
    $string .= "$days" . "d ";
}
if($hours > 0){
    $string .= "$hours" . "h ";
}
if($minutes > 0){
    $string .= "$minutes" . "min ";
}
if ($seconds > 0){
    $string .= "$seconds" . "sec";
}
return $string;
}

 

 

So far so good, however for the itemprop I needed to convert the fields values from minutes to ISO 8601 values that can be then populated. To do so, I used the following function:

Spoiler

function time_to_iso8601($time) {
	$units = array(
		"Y" => 365*24*3600,
		"D" =>     24*3600,
		"H" =>        3600,
		"M" =>          60,
		"S" =>           1,
	);

	$str = "P";
	$istime = false;

	foreach ($units as $unitName => &$unit) {
		$quot  = intval($time / $unit);
		$time -= $quot * $unit;
		$unit  = $quot;
		if ($unit > 0) {
			if (!$istime && in_array($unitName, array("H", "M", "S"))) { // There may be a better way to do this
				$str .= "T";
				$istime = true;
			}
			$str .= strval($unit) . $unitName;
		}
	}
	return $str;
}

 

 

So to speak, if my prep_time field holds 75min, this would be converted to PT1H15M.

If someone knows a shorter/more elegant way of converting minutes to days:hours:min:sec (eliminating the 0 values) and converting the time to ISO 8601 it would be great to hear about it ;)

P.S. Forgot to mention that the minutes-to-human readable timing function uses seconds conversion so in my template I call the function:

<?php convert($page->prep_time * 60); ?>

Not sure if it is the best way, but I know for sure it works ;)

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...