Support Center

How to format a Date field programmatically in Drupal 7

Last Updated: Mar 12, 2014 08:53PM EET
The function format_date formats a date, using a date type or a custom date format string.
format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)

Drupal API: https://api.drupal.org/api/drupal/includes%21common.inc/function/format_date/7
 

Custom formats

In case of a custom format, please check out our two following examples. Please note also that the $timestamp parameter should be a UNIX timestamp. 

- If you have added to your content type (for example) the field_event_date with Date (Unix timestamp), you should use the function format_date as follows in your node template:
 
format_date($node-> field_event_date['und'][0]['value'], 'custom', 'D, d M Y');


- If you have added to your content type (for example) the field_event_date with Date (ISO format), you should use the function format_date as follows in your node template:
 
format_date(strtotime($node-> field_event_date['und'][0]['value']), 'custom', 'D, d M Y');


Regrading the PHP date formats 'D, d M Y', please check this documentation page http://www.php.net/manual/en/datetime.formats.date.php

​In case this article leaves any of your questions or concerns unanswered, please feel welcome to email us or post a public question.

​More than (just) Themes
http://www.morethanthemes.com/

Contact Us

[email protected]
http://assets2.desk.com/
false
morethanthemes
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
false
Invalid characters found
/customer/en/portal/articles/autocomplete