This code will allow you to bypass the default CCK formatter for each date field. That is, you'll be able to print multiple or different date formats in different places in your node.tpl.php file.
In order to reformat the date, you have to grab it from the node, which gives you the ISO format. Then you must convert this ISO format to a date object, and then send that date over to date_format_date to be formatted in the way that you desire. This snippet can be adapted to print many different forms of a date on a single node page.
$date_raw = $field_date[0]['value']; //Get datestamp from cck field, could use $node->whatever
$date_object = date_make_date($date_raw); //Format_date wants a date object, so create one
$formatted_date = date_format_date($date_object,'medium'); //Medium is set up the way you want at /admin/settings/date-time/formats
print $formatted_date;
See theme the date for more information on formatting.
If you want to modify a date based on a custom formatter, you should first retrieve the formatter. This can be done as illustrated beneath, where 'time_only' is the ID of your customer formatter:
<?php
$date_raw = $field_datetime[0]['value']; //Get datestamp from cck field, could use $node->whatever
$date_object = date_make_date($date_raw); //Format_date wants a date object, so create one
$date_formatter = date_formatter_format('time_only', 'field_datetime'); // Retrieves the format
$formatted_date = date_format_date($date_object,'custom',$date_formatter);
print $formatted_date;
?>
Comments
for D5
This section is supposed to be for both D5 and D6?
I couldn't get this syntax to work for a D5 node.tpl.php
Showing alternative syntax would be greatly appreciated. . .
Edit 1:
$node->field_xxxxx_xxx[0]['view']
doesn't work (empty output). substituting 'value' is OK, but outputs ISO format
format_date requires Unix timestamp format, so tried using strtotime()
format_date(strtotime($field_date_event[0]['value']), 'custom', 'l, j F, Y')
had to also add timezone adjustment
Edit 2:
better way:
content_format('field_date_event', $node->field_date_event[0], 'default')
and changed the output format in the field config for the content type
How this could be fit in with the more elegant D6 example above is currently beyond me, but hope someone stuck like me arriving here finds this helpful.
multiple internationalisation formats
How about using the i18n module with multiple formats? The format for dates are different languages is different. Can we use the PHP local function in combination somehow? Or how do we get "dezember" (German) or "décembre" for "december" in a multiple language website?
it does not work for me - in
it does not work for me - in the creation of a cck input field of the type "date" i can choose between "Date" "Datestamp" "Datetime"? i have a multilingual site with events ...
Is this part of my problem? which one i should choose?
and i saw never something like this in cck before:
$element = $node->content['field']['field_date_event']['items'][0];
i only now something like this:
$element = $node->content['field_date_event']['items'][0];
is there a hint for bug-tracking?
thanks momper
hello ideas? thanks momper
hello
ideas?
thanks momper
Simple D6 Example
In a node template file, for a CCK date field named date:
Drupal CCK 6.2 custom example with timezone support
It took me a while to figure this out, so I thought that others may find this useful.
Alan Davison
Tnx
This actually worked for me ...
Any advice to make this work for D7?
I'm struggling to make the date field work the way I need because I need two display formats in one node.
Ideally, I want this to display: "Friday, May 13 at 12:30PM ... Two days and twelve hours from now."
Currently, the only way I can achieve it is with two date fields...and absurdly requiring the user to input the same date/time twice. (Which I readily admit is a dumb idea.)
Do you have any helpful advice?
Use "new DateObject" instead
Use "new DateObject" instead of date_make_date:
The above code makes use of the "short" date format.
Don't forget the timezone
I was struggling with why this wouldn't work for awhile. The above prints the date in UTC. Usuaully you want a specific timezone, such as the timezone the field was saved. In that case you need to set the DateObject as being UTC, and then change it to the timezone you wish to display. If anyone knows of a better way, love to hear it:
Got this working
Thanks for the start! The problem with your code is how you are accessing the field array. If you look at the structure of the array
$date_raw['timezone']
would actually befield_event_datetime['en'][0]['value']['timezone']
Also,new DateObject
did not work for me I useddate_make_date
Here is how it should look:
I did not need the
['en']
D6 and custom format
I created a custom format 'simple' : d/m/Y (without time)
If I do this
I get something like that : Je, 17/06/2010 - 00:00 instead of 17/06/2010
Are other formats taken in consideration or are 'short', 'medium' and 'long' the only accepted values for the second parameter?
How to get your custom formatter
I had the same issue. It seems you need to retrieve your custom formatter first. In the example beneath, I had a custom formatter named "time_only", next to the standard long, medium and short.
If you have no custom formatter defined, but just one to specify in your coding how the format should be, you could use:
How to get from-to in local time?
I spent some time having problems with the code immediately above (and now the first example in the main article), and I wanted to help others who might cause themselves similar problems.
In the first line:
$date_raw = $field_datetime[0]['value']; //Get datestamp from cck field, could use $node->whatever
$field_datetime should be changed to the name of your CCK date field. However, in the third line:
$date_formatter = date_formatter_format('time_only', 'field_datetime'); // Retrieves the format
while 'time_only' should be changed to the name of your custom date format, 'field_datetime' should stay as is and should NOT be changed to the name of your CCK date field. Using the name of your CCK date field will return the correct part of the date, using the right presentation formatting (e.g. Month DD, YYYY HH:MM vs YYYY - MM - DD HH:MM:SS according to your preferences); however, it will be only the From date of a from-to pair, and will be in the UTC timezone rather than your site's timezone.
Works like a charm for days, months and years, but...
This code works like a charm for days, months and years, but when it comes to times, I can't seem to get it to work.
I have a "from" and "to" datetime field which displays both times in the correct custom format (time_only), but the time is completely wrong. I think it might be defaulting to UTC timezone (it's reading 12:00am as 7:00am, but 5:30pm as 12:30am...??). I've tried sdmaxey's recommendation but all that did was blow out the times.
...and printed where it's needed in the tpl.php file:
Like I said, works beautifully for the day, month and year, but for some reason not liking the time. I'm trying to figure out the problem and will post a solution for anyone who comes across the same shenanigans.