I'm trying to work with the output from a CCK date field -- trying to format it into a more human-readable format.

(this is a core 5.1 drupal install, using the 5.x-1.2 versions of CCK and Date)

If I do a simple print:
print $item['value']
I get this:
2007-02-22T18:30:00
Which is the correct timestamp, but I would like to have it adapted to our site's time zone (-0600) and formatted a bit better. I read the documentation on the date module, and it looks like I should be able to use the date_show_date function. But when I make this call:
print date_show_date($item['value'], 'm/d/Y H:i', 'local', '')

I get this:
01/01/2007 00:00

It's somehow only managed to get the year. I've checked over my syntax and tried a bunch of variations on the different other attributes, but it just doesn't seem to be working the way I expect it to work. Any clues?

Comments

isaac77’s picture

see this thread for more information about the problem. Seems to be a bug, or at least a problem with the way date is interacting with aspects of cck.

Workarounds

  • Use the views module. Date fields will get automatically formatted properly in list and table views.
  • Use the php strtotime() function to convert the ISO-date stored in $field_date to a unix timestamp, then use appropriate PHP or drupal functions to re-format. (Conversion to unix timestamp seems to be required before re-formatting, at least with the PHP version (4) I am using.)
  • Make database calls that use mySQL functions to reformat the date. (I am assuming other databases would have equivalent functions.) Probably not the most efficient if you're printing a lot of dates on one page. In my case, though, this seemed to be the best solution. mySQL has very extensive capabilities for re-formatting ISO dates without first converting to UNIX timestamp. I'm NOT an expert, but here's the funciton I wrote:
    function reformat_iso_date_display($ourdate_iso, $style)
    {
    
    /*
    $ourdate_iso = a string containing standard format iso date.
    $style = description of output format desired. See MySQL documentation. Example: '%W,  %M %e, %Y, %l:%i %p'. Quotes around the string should not be passed in to this function--they will be automatically added.
    */
    
    $ourdate_query = 'SELECT DATE_FORMAT(\'' . $ourdate_iso . '\', \'' . $style . '\')';
    
    $ourdate_result = db_query($ourdate_query);
    
    return db_result($ourdate_result, $row = 0);
    
    }	
    
  • Create a $date object from the value of the date field. Documentation for date.module mentions this, but does not explain it well. I'm sure this isn't how things are supposed to work, but here's an example that did function for me:
    
    $date_as_date_object = date_make_date();
    
    date_set_date($date_as_date_object, $field_date[0]['value'], 'US/Eastern', 'local', DATE_ISO);
    
    print date_show_date($date_as_date_object, 'date_format_long', 'local')';
    
    
jghyde’s picture

Assuming that your CCK date field looks like this:

<?php
print $node->field_date[0]['value'];
// outputs something like: 20110802T1200
?>

You can format it with this php snippet:

print date('M j, Y', strtotime(str_replace('T', ' ', $node->field_date[0]['value'])));
// outputs something like this: Jul 13, 2011

Explained:

date() is a php core function

'M j, Y' is the format string sent to php to tell it how to output the formatted date. See the php date() function for other formats

strtotime() is a php core function

str_replace() removes the "T" in the middle of Drupal CCK's "ISO" date string and replaces it with a space.

$node->field_date[0]['value'] is the var that CCK has in the $node object. The name may be different, depending upon how you named it in your cck types "manage fields" page.

I hope this helps!

Joe
http://www.hydeinteractive.com/

Local News Platform Built on Drupal
http://sanangelolive.com/

spenoir’s picture

Am I the only who thinks this is madness? Why are you talking about workarounds to format a date when using a web framework? All I want is the date in a decent format, this is crazy!