Converting to a string

string date ( string format [, int timestamp])

However easy Unix timestamps are in scripts, they are not much good to end users - few people track their dates using numbers of seconds!

As mentioned already, users like to have their dates in all sorts of formats, so PHP gives you the choice to convert timestamps into strings in a number of ways using the date() function.

Date() takes two parameters, with the second one being optional as with strtotime(). Parameter one is a special string containing formatting codes for how you want the timestamp converted. Parameter two is the timestamp you want to convert - if you do not supply it, PHP assumes you want to use the current time.

Parameter one is the key: it is a string of letters from a predefined list of options. You can use other characters in the string, and these are copied directly into the formatted date. If you are trying to put words into the date format that you do not want to be converted into their date equivalent, you need to escape them with a backslash \ . To make things even more confusing, if your escaped letter is an existing escape sequence, then you need to escape it again! If that sounds confusing, don't worry - we will be looking at examples.

Here is the complete list of date format characters. Note that they are case sensitive!

Format character




Lowercase am/pm

am or pm


Uppercase am/pm

AM or PM


Swatch Internet Time

000 to 999


ISO 8601 date, time, and time zone



2-digit day of month, leading zeros

01 to 31


Day string, three letters

Mon, Thu, Sat


Month string, full

January, August


12-hour clock hour, no leading zeros

1 to 12


24-hour clock hour, no leading zeros

0 to 23


12-hour clock hour, leading zeros

01 to 12


24-hour clock hour, leading zeros

00 to 23


Minutes with leading zeros

00 to 59


Is daylight savings time active?

1 if yes, 0 if no


Day of month, no leading zeros

1 to 31


Day string, full

Monday, Saturday


Is it a leap year?

1 if yes, 0 if no


Numeric month, leading zeros

01 to 12


Short month string

Jan, Aug


Numeric month, no leading zeros

1 to 12


Difference from GMT



RFC-822 formatted date

Sat, 22 Dec 1979 17:30 +0000


Seconds, with leading zeros

00 to 59


English ordinal suffix for day number

st, nd, rd, or th


Number of days in month

28 to 31


Time zone for server



Unix Timestamp



Numeric day of week

0 (Sunday), 6 (Saturday)


ISO-8601 week number of year

30 (30th week of the year)


Two-digit representation of year

97, 02


Four-digit representation of year

1997, 2002


Day of year

0 to 366


Time zone offset in seconds

-43200 to 43200

As you can see, there is lots of choice available to you when converting from a timestamp to dates. Here are some examples of the format characters in use:

    print date("H:i") . "\n";
    print "The day yesterday was " . date("l", time() - 86400) . "\n";
    print "The year is " . date("Y") . "\n";
    print date("jS of F Y") . "\n";
    print "My birthday is on a " . date("l", strtotime("22 Dec 2004")) . " this year.\n";
    print date("\M\y b\i\\r\\t\h\d\a\y \i\s o\\n \a l \\t\h\i\s \ye\a\\r. ", strtotime("22 Dec 2004")) . "\n";

The first line is the most basic, and prints out the current time in 24-hour clock format. The second line is slightly more complicated mixing in the output of date() with a text string to get a natural-looking statement.

Line three is very simple - it just prints the current year. But on line four we have a more complicated example, which outputs the date in the format of "22nd of October 2003". Notice that we have the word "of" in there amongst the date format and it has been seamlessly passed through to the output instead of being converted. The reason for this is that lowercase O and lowercase F do not have any formatting purpose in the date function (although this may be changed in the future) so they are just copied straight into output.

On line five you can see we have our date function embedded between two other strings, which makes for particularly nice-looking output. It is possible to embed the strings inside the date format, but it ends up looking like line five!

In line six you can see that the same strings from line four are now inside the date format. Most letters in there function as date format symbols so they have been escaped: \M\y, etc. Lowercase B, lowercase O, and lowercase E all have no date format meaning, so they are not escaped. Lowercase R, lowercase T, and lowercase N all have special meanings as escape sequences themselves (carriage return, tab, and new line), which means they need to be double escaped. Notice that each of the escape sequences has two backslashes before them rather than one - that is the key. One backslash stops PHP from reading them as date format characters, and the other one is to stop PHP reading them as escape sequences.

There is a lot you can do with dates, so I strongly recommend that you just play around with different formats to see what you can do with different combinations.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Converting from components >>

Previous chapter: Converting from a string

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.