I have a date field with both a From Date and a To Date, and in a view I would like to break out the date and times separate for theming slifghtly differently than the way it's provided, with just the start and end times in a view:

Saturday, September 18th, 2010
4:00 PM - 11:00 PM

Once I can get the date fields, I can split them apart with a views preprocess function just fine. The problem is getting everything available in the first place. In the views template, $fields[field_time_value']->content has everything, but it's already rendered so it's surrounded by tags:

<span class="date-display-single">Sat, 09/18/2010 - <span class="date-display-start">4:00pm</span><span class="date-display-separator"> - </span><span class="date-display-end">11:00pm</span></span>

and the $fields['field_time_value']->raw only has the start date:

2010-09-18T23:00:00

Is there a way to get both the start date and times in a format that isn't already rendered so I can format everything as I want?

Thanks.

CommentFileSizeAuthor
#6 views field optoins.jpg100.99 KBarlinsandbulte

Comments

danepowell’s picture

Title: Getting date and time from date field » Get raw date and time from field handler in a View

I'm having the same problem- I'm writing a View style plugin, and need to get the unformatted date and time just like you. The first two lines below return the full HTML rendered value, while the last one returns nothing. I would expect pre_render to return a validated but unformatted date... I'd really appreciate some guidance here.

<?php
$view->render_field('date_field_id', $row_number);
$view->field['date_field_id']->render($row_result);
$view->field['date_field_id']->pre_render($row_result);
?>
danepowell’s picture

You can of course get the raw value directly from each $row in $view->result using the field alias-

<?php
$date_field_alias = $view->display_handler->get_handler('field', 'date_field_id')->field_alias;
foreach ($view->result as $row_number => $row_result) {
  print($row_result->$date_field_alias);
}
?>

... but this is very hackish I think, and generally discouraged as it avoids all built-in validation/processing/formatting mechanisms. Which may be what you want I suppose, but I'd prefer to have at least basic validation, just no formatting. Plus, that's a heck of a lot more code than just using something like <?php $view->field['date_field_id']->pre_render($row_result); ?>

wonder95’s picture

I spoke with merlinofchaos and dereine in IRC, and they said the best thing to do would be to write a CCK formatter that would output the data in a usable format. I personally think that just a Unix timestamp would be best, because you can then just use format_date() to get exactly what you need.

arlinsandbulte’s picture

Maybe I am way off base, but why not just use various date formats? Set up your formats here: /admin/settings/date-time/formats. You might set up a format that only has the date, only the time, only the hour, etc.
Then, you can add multiple date fields to your views fields list, each with a different format
Then, display and theme them however you wish.

wonder95’s picture

Status: Active » Fixed

@arlinsandbulte, thanks for the suggestion. I should have thought about that. I was able to get what I wanted to by adding another instance of the date field, setting it to a date format that displayed the time only, and then selecting the "Display From and To dates" option. This gave me the content of

<span class="date-display-single"><span class="date-display-start">8:00am</span><span class="date-display-separator"> - </span><span class="date-display-end">2:00pm</span></span>

which worked fine for what I needed. However, I could see a case where I still need to split the start time apart from the end time, and in that case a CCK formatter would be needed.

arlinsandbulte’s picture

StatusFileSize
new100.99 KB

Glad to see you get what you wanted.

To split the start time from the end time, I think you can still do it with formats.
In the views field display settings, there is an option below the Format option to either:
1.) Display from and To dates
2.) Display from date only
3.) Display to date only

Would that get you the rest of the way there?

wonder95’s picture

Ah, yes, I suppose that would work. I would just have to add a third instance of the date field, set the format the same on both, and then set the second as "From date only" and the third as "To date only".

arlinsandbulte’s picture

Exactly.

danepowell’s picture

Unless I'm missing something here, you're still getting the date wrapped in all of the HTML, correct? Didn't you want it in a more raw format?

wonder95’s picture

Yes, that's true. If all I need to do is place it somewhere on a page without necessarily formatting it, then this works fine (as is true with my current use case - see the left sidebar here). However, if I need to do some more fine grained formatting (like I did here), then I would need to get it in a raw format, and that's where a CCK formatter that puts out a Unix timestamp would be best.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

rbrownell’s picture

I don't know if this helps, but you should select "Strip HTML tags" in the options for the view. It worked for me.

leewoodman’s picture

This [#4] is exactly what i needed thanks