1

I have a Date field on a content type. I'm trying to render it so that just the Month and Day are visible, but I need each in a separate .

I think the most appropriate place to do this is in the preprocess function for the node theme function. Within that function, here's the date field on the node object:

enter image description here

What is the appropriate way to convert this to something I can run a custom date formatter on, so I can get the month and the day separately?

This is my current approach:

$date_field = $node->field_event_date_time[$node->language][0];
$date_object = new DateObject($date_field['value'], new DateTimeZone($date_field['timezone_db']));

$variables['event_month'] = date_format_date($date_object, 'custom', 'M');
$variables['event_day'] = date_format_date($date_object, 'custom', 'j');

But it does seem a bit messy, and I've noticed is that the field won't always have the language array key, so that complicates it a bit further.

  • I think u cn use php explode function – harsh_behl_0007 Aug 19 '14 at 16:10
  • Wrap the node in an Entity metadata wrapper to get to the date field, or use field_get_items. – J. Reynolds Aug 19 '14 at 16:24
0

If your current approach works, consider sticking with it. Use field_get_items() to get past the "field won't always have the language" problem.

To answer the original question, at admin/config/regional/date-time you'll find a tab to create a new format. You can somewhat laboriously create a format based on something like:

<\s\p\a\n \c\l\a\s\s="\f\o\o">l F j</\s\p\a\n> - <\s\p\a\n \c\l\a\s\s="\b\a\r">g:i a</\s\p\a\n>

All the backslashes may not be necessary, but you have to backslash escape any character that is a special date code.

Then edit a type or make a new one that uses your format with the markup built in.

0

You can use theme hooks to override the format of a specific date format as follows:

/**
 * Override theme_date_display_single().
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 */
function theme_date_display_single($variables) {
  if($variables['dates']['format'] == "d M Y"){
    // In this case i keep only start date. For keep end date use value2.
    $date = $variables['dates']['value']['formatted_iso'];
    $timezone = $variables['timezone'];
    $attributes = $variables['attributes'];
    // Set my vars
    $day = format_date(strtotime($date), 'custom', 'd');
    $month = format_date(strtotime($date), 'custom', 'M');
    $year = format_date(strtotime($date), 'custom', 'Y');

    // Wrap the result with the attributes.
    return '<span class="date-display-single"' . drupal_attributes($attributes) . '>' . '<span class="d">' . $day . '</span>' . '<span class="M">' . $month . '</span>' . '<span class="Y">' . $year . '</span>' . $timezone . '</span>';
  }
}

This solution has been posted here: https://www.drupal.org/node/423870#comment-9871591

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.