Name

date()

Synopsis

    string date ( string date_format [, int timestamp] )

Users like to have their dates in a variety of formats, so PHP lets you convert timestamps into different types of strings using the date() function.

You can send two parameters to date(), 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, and parameter two is the timestamp you want to convert. If you do not supply the second parameter, PHP assumes you want to convert the current time.

Parameter one is tricky: it is a string of letters from a predefined list of 31 possibles. 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!

The complete list of date format characters is shown in Table 7-1. Be careful, as they are case-sensitive!

Table 7-1. Format characters for use in date()

Format character

Description

Example

a

Lowercase am/pm

am or pm

A

Uppercase am/pm

AM or PM

B

Swatch Internet Time

000 to 999

c

ISO 8601 date, time, and time zone

2004-06-18T09:26:55+01:00

d

2-digit day of month, leading zeros

01 to 31

D

Day string, three letters

Mon, Thu, Sat

F

Month ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.