2

I am building a Recipes site. As Google is demanding here: https://support.google.com/webmasters/answer/173379?hl=en I need to collect prepTime, cookTime and totalTime, in ISO 8601 duration format.

I think I found a way to do that:
I have installed the date module.
Then I have an option to choose field type: "Date (ISO format)".
For field widget I chose "select list".
Then, in the field settings "MORE SETTINGS AND VALUES" -> "Custom format" -> and writing "H:i".
For displaying the field I am choosing: "Date and Time" -> Choose how users view dates and times: "Hours and Minutes (00:10)".
For me it looks fine, and it's collecting the data as I want, and displaying it properly.

Does my way is correct?
Will Google be able to read this data from the field correctly?

I am using the Microdata module and the Schema.org module. So, for example, in the field setting of the "Cook Time" field, I defined the relevant Property ("cookTime") in the microdata section and in the schema.org section. Is that enough? Does now Google will be able to read this data from the field correctly?

My site is still under development, using acquia DEV desktop. But, Here is a Sample text that I copied from the "view source":

<div class="panel-pane pane-entity-field pane-node-field-preptime" >

        <h2 class="pane-title">Prep Time</h2>


  <div class="field field-name-field-preptime field-type-date field-label-hidden"><div class="field-items" id="md9"><div class="field-item even" itemprop="prepTime"><span class="date-display-single" property="schema:prepTime" datatype="xsd:dateTime" content="0000-01-01T03:45:00+01:39">03:45</span></div></div></div>

  </div>

<div class="panel-pane pane-entity-field pane-node-field-cooktime" >

        <h2 class="pane-title">Cook Time</h2>


  <div class="field field-name-field-cooktime field-type-date field-label-hidden"><div class="field-items" id="md11"><div class="field-item even" itemprop="cookTime"><span class="date-display-single" property="schema:cookTime" datatype="xsd:dateTime" content="0000-01-01T01:15:00+01:39">01:15</span></div></div></div>

  </div>


Update:
I created the following field override template file called: "field--field_preptime.tpl.php"

<?php
function time_to_iso8601_duration($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;
}
 ?>


<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>    
    <?php foreach ($items as $delta => $item): ?>
    <?php $time = strtotime(render($item), 0); ?>
      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>></div>
          <time datetime=<?php print time_to_iso8601_duration($time); ?> itemprop="prepTime"><?php print render($item); ?></time>
    <?php endforeach; ?>
  </div>
</div>

The Output, when I am viewing in the "view source" on the relevant page:

<div class="field field-name-field-preptime field-type-number-integer field-label-hidden">
    <div id="md11" class="field-items">    
              <div itemprop="prepTime" class="field-item even"></div>
          <time datetime="PT3H45M" itemprop="prepTime">3 hours 45 min</time>
      </div>
</div>

As far as I can see, the input is fine, and the output is fine.
What do you think?
Does now everything is correct, and Google can read it now correctly?

1
  • Hello All. I update the Question with a solution. I will be Happy to get a review and insights for the solution. Is it correct? Does now Google will be able to read it correctly? Thanks.
    – EB84
    Nov 12 '13 at 21:55
3

The page to you link to: Rich snippets - Recipes, tells you to use the ISO 8601 duration format attached to the properties prepTime, cookTime, and totalTime in one of three standard metadata formats (Microdata, Microformats, or RDFa).

What you're proposing do will not enable Google use these duration data for a rich snippet. However, Google may still index your site and use your data without using this special format - it then fall back on the human-readable data. Not having a your data packaged for a rich snippet is usually not a major problem vis a vis Google. You need to think about offering Google data prepared for a rich snippet is worth the work required to render these data.

As for using the Microdata and Schema.org modules to inject this metadata, it may or may not work. It depends on the capabilities of the modules, and on how you use these modules.

When looking at the output posted in your question, using MicroData, you output:

<div class="field-item even" itemprop="cookTime">
  <span class="date-display-single" property="schema:cookTime" datatype="xsd:dateTime"
    content="0000-01-01T01:15:00+01:39">01:15</span>
</div>

while Google expects this:

<time itemprop="cookTime" datetime="PT1H15M">01:15</time>

Hence, I doubt that the current output produced by the MicroData will be understood by Google. You've got the itemprop="cookTime" right, and the div-wrapper is probably also OK, but you're missing the datetime attribute in the MicroData tag and the formatting of the machine readable duration is wrong.

I think it would be simpler to use a template override to inject these metadata tags in the HTML, instead of struggling with these modules. So if you want to do this, here is how I would have done it:

The ISO 8601 duration format is not intened to be seen by humans. For instance, to indicate a duration of ten minutes the format is: "PT10M" and 3 hours 30 minutes is "PT3H30M". You can generate interval objects with the PHP DateInterval class. Example:

$interval = DateInterval::createFromDateString('3 hours 30 minutes');
$isointerval = sprintf('PT%dH%dM', $interval->h, $interval->i);

To get Google to pick up these, you need to embed them in the page HTML markup (they're not supposed to be visible to humans) attached to the properties prepTime, cookTime, and totalTime using the syntax one of three metadata schemes (microdata, microformats, or RDFa). You do this in Drupal by either overriding the template of page, node, etc. you want to embed the properties in, or by creating a custom field module that implements the cooking duration field complete with the markup specified by Google.

3
  • I thank you very much for your help. Unfortunately, I have no Idea how to overriding, and set the code to be as you suggested, even after reading the guide and seeing your example. I will have to read and understand much more before doing that. foe example, I need to figure out what to put parameter instead of "3 hours 30 minutes". although I have a little experience in coding, I did not code in Drupal, yet. I will be much obliged, if you have more sources/links/tutorials that you can send me, and will help me doing that?
    – EB84
    Nov 12 '13 at 7:18
  • I also read the following page: stackoverflow.com/questions/13301142/…. It also looks like a solution, But I Really don't know how to use that and where to put it..
    – EB84
    Nov 12 '13 at 7:56
  • Hey. Please see the update in the question. I override the field.tpl.php, and it's looks like you said it should be. But, I will be more than happy to get your insights. Thanks.
    – EB84
    Nov 12 '13 at 21:32
2

No, you will need to include additional information for ISO duration format.

http://en.wikipedia.org/wiki/ISO_8601#Durations

You can use various metadata formats for Recipes to add the machine readable information for indexing, e.g microdata, microformat or RDFa

e.g, microdata for recipes.

<div itemscope itemtype="http://data-vocabulary.org/Recipe" >
  <!-- other content here -->
  Prep time: <time datetime="PT30M" itemprop="prepTime">30 min</time>
  Cook time: <time datetime="PT1H" itemprop="cookTime">1 hour</time>
  Total time: <time datetime="PT1H30M" itemprop="totalTime">1 hour 30 min</time>
  <!-- other content here -->
</div>

See the section at the bottom of this page: https://support.google.com/webmasters/answer/173379?hl=en for more information on metadata formats.

You'll probably need to use custom field templates or similar to add the additional metadata information there.

3
  • I am using the microdata module and the schemaorg module.Sorry for not mentioning that. I was just speaking on the data collection in ISO 8601 duration foramt and displaying of the data for the users. So, if I am using the microdata module and the schemaorg module, I am good to go?
    – EB84
    Nov 11 '13 at 23:26
  • You should show the human-readable date format to your users, e.g H:i the HTML element tag can then include the machine-readable date format for Google etc... e.g datetime="PT30M" Nov 12 '13 at 0:37
  • Hey. Please see the update in the question. I did as you also suggested, and it's looks like you said. Anyway, I will be more than happy to get your comments. Thanks.
    – EB84
    Nov 12 '13 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.