Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page