How to generate a time element from a timestamp in Drupal

Here's how to create a Drupal 7 render array for a <time> element from a timestamp in PHP. In the example below, the theme() function is called directly. Most likely you'll want to create a render array instead. This pattern can be used to create any html element.

Note that time is already in Drupal 8.

Also note that this can be combined with the date module's theme_date_display_single() function.

<?php
 
  theme('html_tag', array(
    'element' => array(
      '#tag' => 'time',
      '#attributes' => array(
        'property' => 'dc:date',
        'datatype' => 'xsd:dateTime',
        'content' => date(DATE_W3C, $variables['created']),
        'datetime' => date(DATE_W3C, $variables['created'])
      ),
      '#value' => date('M d, Y', $variables['created']),
    ),
  ));

Tags

External References

Article Type

General