Home > Articles

  • Print
  • + Share This

Formatting Timestamps

Problem

You want to format a timestamp in string format, such as the MM/DD/YYYY format discussed earlier in this chapter.

Solution

The most efficient way of formatting a UNIX timestamp is to use the date() function or the strftime() function, both of which take an optional timestamp:

<?php
// The day I was born
$date = date ("l F j", 404107200);

// Same thing, sensitive to locale settings
$date1 = strftime ("%A %B %d", 404107200);
?>

Discussion

This recipe is a complete discussion of two functions with which you are already familiar. These functions are probably all that you need for printing out dates and timestamps in the format you choose.

First, let us talk about the date() function. As its first argument, the date() function takes a bunch of formatting codes interspersed with text. Here is a list of formatting codes for the date() function:

  • a"am" or "pm"

  • A"AM" or "PM"

  • d—Day of the month, two digits with leading zeros; that is, "01" to "31"

  • D—Day of the week, textual, three letters; for instance, "Fri"

  • F—Month, textual, long; for instance, "January"

  • h—Hour, 12-hour format; that is, "01" to "12"

  • H—Hour, 24-hour format; that is, "00" to "23"

  • g—Hour, 12-hour format without leading zeros; that is, "1" to "12"

  • G—Hour, 24-hour format without leading zeros; that is, "0" to "23"

  • i—Minutes; that is, "00" to "59"

  • j—Day of the month without leading zeros; that is, "1" to "31"

  • l (lowercase L)—Day of the week, textual, long; for instance, "Friday"

  • L—Boolean for whether it is a leap year; "0" or "1"

  • m—Month; that is, "01" to "12"

  • n—Month without leading zeros; that is, "1" to "12"

  • M—Month, textual, 3 letters; for instance, "Jan"

  • s—Seconds; that is, "00" to "59"

  • S—English ordinal suffix, textual, 2 characters; that is, "th" and "nd"

  • t—Number of days in the given month; that is, "28" to "31"

  • U—Seconds since the UNIX epoch, January 1, 1970

  • w—Day of the week, numeric; that is, "0" (Sunday) to "6" (Saturday)

  • Y—Year, 4 digits; for example, "1999"

  • y—Year, 2 digits; for example, "99"

  • z—Day of the year; that is, "0" to "365"

  • Z—Time zone offset in seconds; that is, "-43200" to "43200"

You can put these codes into the date() function along with regular characters:

<?php
print date ("n/j/Y");
?>

This would print out the date in Month/Day/Year format where Month is the current month, Day is the current day, and Year is the current year.

The date() function also takes an additional timestamp which is, as mentioned in the previous recipe, the seconds since the UNIX epoch began (January 1, 1970). "What?? I have to count the seconds since January 1, 1970?" No! Most UNIX timestamps are already in that format, so you just read them into the date() function. However, if you would have a need to count the seconds from January 1, 1970, just use the mktime() function to do it for you. Here is the format of the mktime() function:

int mktime(int hour, int minute, int second, int month,
 int day, int year, int [is_dst]);

Most of the parameters are self-explanatory. The optional parameter [is_dst] specifies whether mktime() should account for daylight saving time.

The strftime() function is a lot like the date() function. The only differences are the formatting codes are different and the strftime() function is sensitive to locale settings that are set by setlocale(). So, you can have the string formatted for, Germany, if you have set the locale to German (de). For information on how strftime() can be used with setlocale(), check out recipe 3.1. Here is a list of the different formatting codes passed to strftime():

  • %a—Abbreviated weekday name according to the current locale

  • %A—Full weekday name according to the current locale

  • %b—Abbreviated month name according to the current locale

  • %B—Full month name according to the current locale

  • %c—Preferred date and time representation for the current locale

  • %d—Day of the month as a decimal number (range 00 to 31)

  • %H—Hour as a decimal number using a 24-hour clock (range 00 to 23)

  • %I—Hour as a decimal number using a 12-hour clock (range 01 to 12)

  • %j—Day of the year as a decimal number (range 001 to 366)

  • %m—Month as a decimal number (range 1 to 12)

  • %M—Minute as a decimal number

  • %p—Either 'am' or 'pm' according to the given time value, or the corresponding strings for the current locale

  • %S—Second as a decimal number

  • %U—Week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week

  • %W—Week number of the current year as a decimal number, starting with the first Monday as the first day of the first week

  • %w—Day of the week as a decimal, Sunday being 0

  • %x—Preferred date representation for the current locale without the time

  • %X—Preferred time representation for the current locale without the date

  • %y—Year as a decimal number without a century (range 00 to 99)

  • %Y—Year as a decimal number including the century

  • %Z—Time zone or name or abbreviation

  • %%—A literal '%' character

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.