Date and Calendar Module in Drupal 5: Part 1

0
223
4 min read

Recipe 33: Understanding Date formats

Drupal dates are typically stored in one of two ways. Core Drupal dates—including Node: Created Time, and Node: Updated Time—are stored as Unix timestamps. Contributed module date fields can be stored as either a timestamp or a format known as ISO. Neither style is particularly friendly to human readers, so both field types are usually formatted before users see them. This recipe offers a tour of places in Drupal where dates can be formatted and information on how to customize the formats.

What’s that Lucky Day?
The Unix timestamp 1234567890 fell on Friday the 13th, in February, 2009. This timestamp marks 1,234,567,890 seconds since January 1, 1970. The same date/time combination would be stored in a date field in ISO format as 2009-02-13T23:31:30+00:0. ISO is an abbreviation for the International Organization for Standardization

  1. Opening the browser windows side-by-side will help you understand date formatting. In the left window, open YOURSITE.com/admin/settings/ date-time to view the settings page for date and time. In the right window, open the API page of code that defines these system date time settings at http://api.drupal.org/api/function/system_date_time_settings/5. Compare each item in the $datemedium array, for instance, with the associated Medium date format drop-down.
    • a – am/pm
    • D – Day, Mon through Sun
    • d – Date, 01 to 31 (with leading zeroes)
    • F – Month, January through December (mnemonic, F = Full name)
    • g – Hours, 1 through 12
    • H – Hours, 00 through 23
    • i – Minutes, 00 to 59
    • j – Date, 1 to 31 (No leading zeroes)
    • l – Sunday through Saturday
    • m – Month, 01 through 12
    • M – Month, Jan through Dec
    • s – Seconds, 00 through 59 (with leading zeroes)
    • S – Month Suffix, st, nd, rd, or th. Works well with j
    • Y – Year, Examples: 1999 or 2011
  2. Drupal 5 Views Recipes

    Below is the list of codes for many commonly used date and time formats. A more comprehensive list appears at http://us.php.net/date.

  1. Explore Drupal places where these codes may be used. The first four locations in the table below are available in the Drupal administrative interface. The last three involve editing files on the server—these edits are completely optional.

Location

Details

CCK field setup

Custom Input formats

admin/content/types/story/add_field

 

Drupal 5 Views Recipes

After the field widget is specified

admin/content/types/<CONTENTTYPE>/fields/field_<FIELDNAME>

Near the top of the page.

 

Drupal 5 Views Recipes

Near the bottom of the page:

 

Drupal 5 Views Recipes

Formatting Fields in Views.

admin/build/views/<VIEW_NAME>/edit

CCK Date fields are set via the Options drop-down in the Fields fieldset.

 

Drupal 5 Views Recipes

Custom date formats for core fields, such as Node: Created Time are set via handler and options from elements.

 

Drupal 5 Views Recipes

Default Date and Time settings

admin/settings/date-time

Set the default time zone, Short, Medium, and Long date formats, and the first day of the week.

 

Post Settings

This may be one of the harder-to-find settings in Drupal, enabling the Post settings to be turned-off for specified content types. (An example of a post setting would be: Submitted by admin on Sun, 10/12/2008 – 4:55pm.

The setting is found on the right-hand side of this URL: admin/build/themes/settings

Use the following mouse click trail to get to this URL:

Administer | Site Building | Themes | Configure

(Click on the Configure tab at the top of the page. If you click on the Configure link in the Operations column, you will still need to click the Configure tab at the top to get to the global settings.)

 

Drupal 5 Views Recipes

Variable overrides in settings.php

You may override variables at the bottom of the /sites/default/settings.php file. Remove the appropriate pound signs to enable the $conf array, and add a setting as shown below. Note that this is a quick way to modify the post settings format, which draws from the medium date variable.

$conf = array(

#   ‘site_name’ => ‘My Drupal site’,

#   ‘theme_default’ => ‘minnelli’,

#   ‘anonymous’ => ‘Visitor’,

‘date_format_medium’ => ‘l F d, Y’

 );

  • *.tpl.php files

Examples:

node-story.tpl.php

<?php print format_date($node->created, ‘custom’, ‘F Y’); ?>

  • comment.tpl.php

<?php echo t(‘On ‘) . format_date($comment->timestamp,
  ‘custom’  , ‘F jS, Y’); ?> <?php echo theme(‘username’,
  $comment) . t(‘ says:’); ?>

  • template.php

Redefine $variables[‘submitted’]

Example from blommor01 theme:

  $vars[‘submitted’] =  t(‘!user – <abbr class=”created”
  title=”!microdate”>!date</abbr>’, array(

   ‘!user’ => theme(‘username’, $vars[‘node’]),

   ‘!date’ => format_date($vars[‘node’]->created),

   ‘!microdate’ => format_date($vars[‘node’]->
  created,’custom’, “Y-m-dTH:i:sO”)

  ));

Recipe notes

Note that when using the PHP date codes, additional characters may be added, including commas, spaces, and letters. In the template.php example, a backslash was used to show that the letter ‘T’ will be printed, rather than the formatted return values. Below are more examples of added characters:

F j, Y, g:i a // August 27, 2010, 5:16 pm
m.d.y // 08.27.10

You may occasionally find that an online date converter comes in handy.

LEAVE A REPLY

Please enter your comment!
Please enter your name here