Support

Account

Home Forums General Issues Entering and Displaying Time Duration

Solved

Entering and Displaying Time Duration

  • I’m working on a recipe database using ACF. Several of the items that we need to be able to include are things like prep time, cook time, and total time.

    These are always displayed in the form of “X minutes” or “X hours Y minutes.” But we also need to be able to add the schema.org recipe markup (http://www.schema.org/Recipe), which requires the time to be coded in the ISO 8601 duration format.

    For example, a recipe that takes 1 hour and 25 minutes would be coded as PT1H25M. A recipe that takes 10 minutes would be coded as PT10M.

    That’s pretty straightforward, but I’m trying to find a way to make the user-entry part of this as elegant as possible. I first considered asking the entry to be only in minutes, but then if it’s a multi-hour recipe, that doesn’t work. But I’d also prefer not to have separate “hour” and “minute” fields for each of these times.

    Any suggestions on how to do this more gracefully? Thanks!

  • Hi @awilder

    The best solution is to create a custom field type for you specific needs.
    You can read how this is possible here:
    http://www.advancedcustomfields.com/resources/tutorials/creating-a-new-field-type/

    If a faster approach is needed, I would create 2 select fields; one for hour and the other for minutes.

    The label for each value would look pretty like “2 hours”, but the value it saves would be “2”. This way, you can construct the schema.org recipe markup like so:

    
    <?php 
    
    $hours = get_field('hours');
    $minutes = get_field('minutes');
    
    $markup = 'PT';
    
    if( $hours )
    {
    	$markup .= $hours . 'H'
    }
    
    if( $minutes )
    {
    	$markup .= $minutes . 'M'
    }
    
    echo $markup;
    
    ?>
    
  • Since i needed something like this, I found a very simple way to accomplish this:
    1. Create a time picker field with return and display format set to H:i
    2. If is a front end form hide “now” button with css.
    3. use this in your template to get time

    <?php 
    $preptime = get_field('prep_time');
    $preparr = explode(':', $preptime);
    $hour = $preparr[0];
    $min = $preparr[1];
    echo '<time content="PT'.$hour.'H'.$min.'M" itemprop="prepTime">'.$preptime.'</time>';
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Entering and Displaying Time Duration’ is closed to new replies.