5

I have a date field. In my template I want to display the date in two different ways. I know in display I can set the format used for a date. To print the date in my template I am doing.

<?php print $node->field_date[0]['view'] ?>

To print as a different format I tried

<?php print format_date($node->field_date[0]['timestamp'],"custom","H:i")?>

However this does not seem to be showing the correct time...

5

We tend to use:


print format_date(strtotime($node->field_date[0]['value']), 'custom', 'H:i');

Perhaps that would work in your situation?

0

One would think that adding logic to honor the user's timezone would do the trick, like this:

format_date($timestamp, 'custom', 'g:i a, d F, Y', variable_get('date_default_timezone', 0));

but I've found in actual practice that this works better:

format_date($timestamp, 'custom', 'g:i a, d F, Y',0);

Passing a "timezone" parameter of 0 causes the D6 logic to get the user's configured timezone, or if user configured timezones are not enabled it does the same thing as my first example. So, if you have not, enable user configured timezones and use my second example.

Nice benefit, different users viewing the same info see date/times in their timezone.

  • 2
    Actually, format_date() will use the user timezone if the $timezone parameter is not set (which means it is NULL, not 0). The default value for that parameter is NULL; it's enough to not pass any value for that parameter, and Drupal will consider the timezone set for the currently logged in user. – kiamlaluno Apr 14 '11 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy