Skip to content

Instantly share code, notes, and snippets.

@reubenmoes
Last active August 29, 2015 14:18
Show Gist options
  • Save reubenmoes/ef129ab50fe8f971ae61 to your computer and use it in GitHub Desktop.
Save reubenmoes/ef129ab50fe8f971ae61 to your computer and use it in GitHub Desktop.
Drupal: theme_date_display_range with a human friendly date.
//
//implements theme_date_display_range()
//A more human friendly date range
//Example output:
//Oct 8 to 15, 2015
//Oct 8 to Nov 3, 2015
//Oct 8, 2015 to Jan 3, 2016
function dnv_date_display_range($variables) {
$timezone = $variables['timezone'];
$attributes_start = $variables['attributes_start'];
$attributes_end = $variables['attributes_end'];
$start_time = strtotime($variables['dates']['value']['formatted_iso']);
$end_time = strtotime($variables['dates']['value2']['formatted_iso']);
//Different year
if ((date('Y', $start_time) != date('Y', $end_time))) {
$date1 = date('M j, Y', $start_time);
$date2 = date('M j, Y', $end_time);
}
//Different Month of the same year
elseif ((date('F', $start_time) != date('F', $end_time))) {
$date1 = date('M j', $start_time);
$date2 = date('M j, Y', $end_time);
}
//Different day in the month
else {
$date1 = date('M j', $start_time);
$date2 = date('j, Y', $end_time);
}
$start_date = '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $date1 . '</span>';
$end_date = '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $date2 . $timezone . '</span>';
// If microdata attributes for the start date property have been passed in,
// add the microdata in meta tags.
if (!empty($variables['add_microdata'])) {
$start_date .= '<meta' . drupal_attributes($variables['microdata']['value']['#attributes']) . '/>';
$end_date .= '<meta' . drupal_attributes($variables['microdata']['value2']['#attributes']) . '/>';
}
// Wrap the result with the attributes.
return t('!start-date to !end-date', array(
'!start-date' => $start_date,
'!end-date' => $end_date,
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment