Skip to Content
PHP Cookbook
book

PHP Cookbook

by David Sklar, Adam Trachtenberg
November 2002
Intermediate to advanced
640 pages
16h 33m
English
O'Reilly Media, Inc.
Content preview from PHP Cookbook

3.4. Printing a Date or Time in a Specified Format

Problem

You need to print out a date or time formatted in a particular way.

Solution

Use date( ) or strftime( ):

print strftime('%c');
print date('m/d/Y');
Tue Jul 30 11:31:08 2002
               07/30/2002

Discussion

Both date( ) and strftime( ) are flexible functions that can produce a formatted time string with a variety of components. The formatting characters for these functions are listed in Table 3-3. The Windows column indicates whether the formatting character is supported by strftime( ) on Windows systems.

Table 3-3. strftime( ) and date( ) format characters

Type

strftime( )

date( )

Description

Range

Windows

Hour

%H

H

Hour, numeric, 24-hour clock

00-23

Yes

Hour

%I

h

Hour, numeric, 12-hour clock

01-12

Yes

Hour

%k

Hour, numeric, 24-hour clock, leading zero as space

0-23

No

Hour

%l

Hour, numeric, 12-hour clock, leading zero as space

1-12

No

Hour

%p

A

AM or PM designation for current locale

Yes

Hour

%P

a

am/pm designation for current locale

No

Hour

G

Hour, numeric, 24-hour clock, leading zero trimmed

0-23

No

Hour

g

Hour, numeric, 12-hour clock, leading zero trimmed

0-1

No

Minute

%M

I

Minute, numeric

00-59

Yes

Second

%S

s

Second, numeric

00-61[a]

Yes

Day

%d

d

Day of the month, numeric

01-31

Yes

Day

%e

Day of the month, numeric, leading zero as space

1-31

No

Day

%j

z

Day of the year, numeric

001-366 for strftime( )

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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
PHP Cookbook, 2nd Edition

PHP Cookbook, 2nd Edition

Adam Trachtenberg, David Sklar
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 1565926811Catalog PageErrata