Community

Weird Date Display in CCK / Contemplate

Hi There -

Am having difficulty with theming a date. I've searched for some time and people don't seem to be having this problem. I'm trying to get it to just print out normally, like a normal date. My client wants it in MM/YY. First I need to get it to just print out.

In CCK, the node has a date field that's a dropdown. When I go into Contemplate variables, I get the following:

$node->field_event_date (array)

    $node->field_event_date[0] (array)

        $node->field_event_date[0]['value'] **
            2008-02-13T00:00:00

Trying to break out the date using the array thusly yields: (the result is after the statement)
<?php print check_plain($node->field_event_date[0]['value']) ?> yields: 2008-02-13T00:00:00
<?php print check_plain($node->field_event_date[0]['value']['mon']) ?> yields: 2 <-----Month with no '0' in front.
<?php print check_plain($node->field_event_date[0]['value']['year']) ?> yields: 2<----- ???Month with no '0' in front, not year. Okay let's try another method.

So then I tried using the format_date function:

<?php
$time
= check_plain($node-> field_event_date[0]['value']);?>

<?php print $time; ?> yields: 2008-02-13T00:00:00
<?php print format_date($time, 'custom', 'm\/y' ); ?> yields: 12/69 <-----right format!
<?php print format_date($time); ?> yields: Wed, 12/31/1969 - 19:33
<?php print format_date($time, 'small'); ?> yields: 12/31/1969 - 19:33
<?php print format_date($time, 'medium'); ?> yields: Wed, 12/31/1969 - 19:33
<?php print format_date($time, 'large'); ?> yields: Wednesday, December 31, 1969 - 19:33

Huh? Where did Drupal get the notion of Dec 31, 1969? More to the point, how do I get Drupal to recognize this date and format it?

I'm at a loss. I'll take any suggestions on how to go about this.

Comments

Anyone?

Anyone?

I could be wrong, but

I could be wrong, but format_date expects a timestamp, but it looks like the 'value' attribute is not one.

Have you looked here:
http://drupal.org/node/123181

You're right, and I have...

You're right, but I had to try the format_date function. It looked so promising. Thanks for your reply.

Unfortunately the link you sent me to is where I got the idea for my first approach - that and a few other links I've run into. Anyone else have any ideas? It's a straight CCK date field. If there's any way that my question isn't giving you enough data, please let me know.

Another Data Point

Went back to the page that criznach suggested and thought I'd try one more thing:

<?php print check_plain($node->field_event_date[0]['value']['formatted']) ?>

yields '2' as an output.

Anyone else have any ideas?

please? someone?

please? someone?

It's a bug in the way CCK interacts with Date.

Found two valuable threads:

http://drupal.org/node/119879
and
http://drupal.org/node/115706

The first reply on the second thread has a functional workaround.

For Drupal 6:
The datetime CCK "value" is being returned as a string so use strtotime to convert it to a date to be used by format_date.

e.g.
print format_date(strtotime($node->field_birth_date[0]['value']), 'custom', 'm/d/Y')