Formatting Timestamps in Drupal

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
November 18, 2017, 2:50 am
m.d.y
11.18.17
j, n, Y
18, 11, 2017
Ymd
20171118
\i\t \i\s \t\h\e jS \d\a\y.
it is the 18th day.
D M j G:i:s T Y
Sat Nov 18 2:50:34 GMT 2017
H:i:s
02:50:34
Additional Resources:

Comments

Post new comment

  • Allowed HTML tags: <em> <strong>
  • Lines and paragraphs break automatically.

More information about formatting options

Image CAPTCHA
Enter the characters shown in the image.