Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Formatting Timestamps in Drupal

Parent Feed: 

Most data dealing with times and dates is stored as a as a standard Unix timestamp in Drupal. Examples of this are the node creation and revision dates, or the user creation, login, and access dates. If you access this data directly from the database you will retrieve this highly illegible timestamp. Good thing that Drupal provides a function for formatting dates: format_date().

format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL)

This function will return a formatted date string of the passed timestamp. The type parameter accepts the following strings: small, medium, large, and custom. The first three type parameters can be defined by the user on the Date and Time settings page. If custom is chosen then the format parameter must also be filled in. The format parameter accepts a PHP date format string, possible values follow:

d - Day of the month, 2 digits with leading zeros
D - A textual representation of a day, three letters
j - Day of the month without leading zeros
l - A full textual representation of the day of the week
N - ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
S - English ordinal suffix for the day of the month, 2 characters
w - Numeric representation of the day of the week
z - The day of the year (starting from 0)
W - ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
F - A full textual representation of a month, such as January or March
m - Numeric representation of a month, with leading zeros
M - A short textual representation of a month, three letters
n - Numeric representation of a month, without leading zeros
t - Number of days in the given month
L - Whether it's a leap year
Y - A full numeric representation of a year, 4 digits
y - A two digit representation of a year
a - Lowercase Ante meridiem and Post meridiem
A - Uppercase Ante meridiem and Post meridiem
B - Swatch Internet time
g - 12-hour format of an hour without leading zeros
G - 24-hour format of an hour without leading zeros
h - 12-hour format of an hour with leading zeros
H - 24-hour format of an hour with leading zeros
i - Minutes with leading zeros
s - Seconds, with leading zeros
u - Microseconds (added in PHP 5.2.2)
e - Timezone identifier (added in PHP 5.1.0)
I - Whether or not the date is in daylight saving time
O - Difference to Greenwich time (GMT) in hours
P - Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)
T - Timezone abbreviation
Z - Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.

Examples of custom date strings follow all using current time from the time() function for the timestamp:

F j, Y, g:i a
March 14, 2017, 10:50 am
m.d.y
03.14.17
j, n, Y
14, 3, 2017
Ymd
20170314
\i\t \i\s \t\h\e jS \d\a\y.
it is the 14th day.
D M j G:i:s T Y
Tue Mar 14 10:50:06 GMT 2017
H:i:s
10:50:06
Additional Resources:
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web