What is the right way to have a custom format in tpl? I have no problem with D6 but apparently it change for Drupal 7.

In the node array in my node.tpl I have something like:

<?
[field_my_date] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 2011-01-20T23:00:00
[timezone] => Europe/Berlin
[timezone_db] => Europe/Berlin
[date_type] => date
)
)
)
?>

But how do I print it?

Thanks for your help

Comments

Anonymous’s picture

subscribe

gagarine’s picture

render() doesnt work neither...

valderama’s picture

good question :)

i tried

format_date(strtotime($field_news_date['de'][0]['value']), 'medium', '', 'Europe/Paris');

but the timezone seems not to be applied (all dates are two hours early)

gagarine’s picture

Component: Documentation » Date API

more about the API...

adaddinsane’s picture

I have a field for date-of-birth (field_dob) which has Y-m-d granularity, so no timezone conversion. But when it's stored it's subtracting an hour - because I'm in the UK on British Summer Time, presumably.

So a field_dob might look like this:

value (String, 19 characters ) 1958-09-21T23:00:00
timezone (String, 13 characters ) Europe/London
timezone_db (String, 3 characters ) UTC
date_type (String, 4 characters ) date

Where the date of birth was 22nd Sept 1958. I then put the date into a DateObject:

$dobj = new DateObject($item['value'], $item['timezone']);

Which doesn't give me the right answer. So.....

Working from the basic principle that the date module knows how to get it right :-) I did some investigating and I can see what we need is the code in the helper function date_formatter_process() in the date.module

It's going to be ugly.

Ah well.

cappadona’s picture

subscribe

joelcollinsdc’s picture

subscribe

joelcollinsdc’s picture

Can someone tell me if this is right? Maybe i'm dense but this has been really frustrating...

I'm just trying to get the date out of the field in a preprocess function. (in the correct timezone)

$db_value = $node->field_event_date[$node->language][0]['value'];
$date = new DateObject($db_value,'UTC',DATE_FORMAT_ISO);
$variables['event_date'] = format_date($date->format('U'),'custom','F j, Y g:i a');

gagarine’s picture

I'm still having this problem... nobody can help about it?.

EDIT if you have the object it's easy you can just do:

$dates['value']['local']['object']->format('Y');

But in the tpl we don't have the object :(.

karens’s picture

Status: Active » Fixed

The formatter has all the values you want. You can create a custom theme to override the current themes (date_display_single and date_display_range) and you will have all the values and you can do anything you like with them.

Status: Fixed » Closed (fixed)

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

michiellucas’s picture

did anyone find a solution,
is there a D7 date_convert function?

adaddinsane’s picture

I do have a solution but it's tied up in a module which I hope to release some time soon - it allows you to fetch field data in a variety of ways and sorts out all these niggling little details. (Sorry I can't do it straight away - real life has me stupidly busy making websites and earning the pennies.)

kork’s picture

Here is my example code to output a custom formated date field:

$field_name='field_date';
$items = field_get_items('node', $node, $field_name);
$field = field_info_field($field_name);
$instance = field_info_instance('node', $field_name, $node->type);
$item=$items[0]; //Just display the first date
$display=field_get_display($instance, 'default', $node);
$display['settings']['format_type']='short'; //Alter the display
$vars['dates'] =date_formatter_process('date_default', 'node', $node, $field, $instance, $langcode, $item, $display);
$output=theme('date_display_combination', $vars);  

you can also use the Date object from the date_formatter_process function.

adaddinsane’s picture

Or you could use my module field_extract which allows you to specify formatters as well as just extracting values correctly.

Just sayin'.

gagarine’s picture

krell’s picture

Using an existing date format preset (short):

print render(field_view_value('node', $node, 'field_my_custom_date', $node->language, array(
  'settings' => array(
    'format_type' => 'short'
  )
)));
fureigh’s picture

Thanks, krell!

(Tried to delete this so I could post a reply instead, but couldn't — pardon.)