Printing a Date or Time in a Specified Format






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( ), as shown in Figure.

Using date( ) and strftime( )

<?php
print strftime('%c');
print date('m/d/Y');
?>

Figure prints something like:

Mon Dec  3 11:31:08 2007
12/03/2007

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 Figure. The Windows column indicates whether the formatting character is supported by strftime( ) on Windows systems.

strftime( ) and date( ) format characters

Type

strftime( )

date( )

Description

Range

Windows

Hour

%H

H

Hour, numeric, 24-hour clock

0023

Yes

Hour

%I

h

Hour, numeric, 12-hour clock

0112

Yes

Hour

%k

 

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

023

No

Hour

%l

 

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

112

No

Hour

%p

A

A.M. or P.M. designation for current locale

 

Yes

Hour

%P

a

A.M. or P.M. designation for current locale

 

No

Hour

 

G

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

023

No

Hour

 

g

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

01

No

Minute

%M

i

Minute, numeric

0059

Yes

Second

%S

s

Second, numeric

0061[]

Yes

Day

%d

d

Day of the month, numeric

0131

Yes

Day

%e

 

Day of the month, numeric, leading zero as space

131

No

Day

%j

z

Day of the year, numeric

001366 for strftime( ); 0365 for date( )

Yes

Day

%u

N

Day of the week, numeric (Monday is 1)

17

No

Day

%w

w

Day of the week, numeric (Sunday is 0)

06

Yes

Day

 

j

Day of the month, numeric, leading zero trimmed

131

No

Day

 

S

English ordinal suffix for day of the month, textual

"st," "th," "nd," "rd"

No

Week

%a

D

Abbreviated weekday name, text for current locale

 

Yes

Week

%A

l

Full weekday name, text for current locale

 

Yes

Week

%U

 

Week number in the year, numeric, first Sunday is the first day of the first week

0053

Yes

Week

%V

W

ISO 8601:1988 week number in the year, numeric, week 1 is the first week that has at least 4 days in the current year, Monday is the first day of the week

0153

No

Week

%W

 

Week number in the year, numeric, first Monday is the first day of the first week

0053

Yes

Month

%B

F

Full month name, text for current locale

 

Yes

Month

%b

M

Abbreviated month name, text for current locale

 

Yes

Month

%h

 

Same as %b

 

No

Month

%m

m

Month, numeric

0112

Yes

Month

 

n

Month, numeric, leading zero trimmed

112

No

Month

 

t

Month length in days, numeric

28, 29, 30, 31

No

Year

%C

 

Century, numeric

0099

No

Year

%g

 

Like %G, but without the century

0099

No

Year

%G

o

ISO 8601 year with century; numeric; the four-digit year corresponding to the ISO week number; same as %y except if the ISO week number belongs to the previous or next year, that year is used instead

 

No

Year

%y

y

Year without century, numeric

0099

Yes

Year

%Y

Y

Year, numeric, including century

 

Yes

Year

 

L

Leap year flag (yes is 1)

0, 1

No

Time zone

%z

O

Hour offset from GMT, ±HHMM (e.g., -0400, +0230)

-1200+1200

Yes, but acts like %Z

Time zone

 

P

Time zone offset including colon (e.g. -04:00, +02:30)

-12:00 +12:00

 

Time zone

%Z

T

Time zone, name, or abbreviation; textual

 

Yes

Time zone

 

e

Timezone identifier, e.g., America/New_York

  

Time zone

 

I

Daylight savings time flag (yes is 1)

0, 1

No

Time zone

 

Z

Seconds offset from GMT; west of GMT is negative, east of GMT is positive

-4320043200

No

Compound

%c

 

Standard date and time format for current locale

 

Yes

Compound

 

c

ISO 8601formatted date and time

 

Yes

Compound

%D

 

Same as %m/%d/%y

 

No

Compound

%F

 

Same as %Y-%m-%d

 

No

Compound

%r

 

Time in A.M. or P.M. notation for current locale

 

No

Compound

%R

 

Time in 24-hour notation for current locale

 

No

Compound

%T

 

Time in 24-hour notation (same as %H:%M:%S)

 

No

Compound

%x

 

Standard date format for current locale (without time)

 

Yes

Compound

%X

 

Standard time format for current locale (without date)

 

Yes

Compound

 

r

RFC 822formatted date (e.g., "Thu, 22 Aug 2002 16:01:07 +0200")

 

No

Other

%s

U

Seconds since the epoch

 

No

Other

 

B

Swatch Internet time

 

No

Formatting

%%

 

Literal % character

 

Yes

Formatting

%n

 

Newline character

 

No

Formatting

%t

 

Tab character

 

No


[] The range for seconds extends to 61 to account for leap seconds.

The c formatting character was added to date( ) in PHP 5.0.0. The N, o, and e characters were added to date( ) in PHP 5.1.0. The P character was added in PHP 5.1.3.

The first argument to each function is a format string, and the second argument is an epoch timestamp. If you leave out the second argument, both functions default to the current date and time. While date( ) and strftime( ) operate over local time, they each have UTC-centric counterparts (gmdate( ) and gmstrftime( )).

In PHP 5.1.1 and later, there are some handy constants that represent the format string to be passed to date( ) for common date formats. These constants are listed in Figure.

Table Constants for use with date( )

Constant

Value

Example

Usage

DATE_ATOM

Y-m-d\TH:i:sO

2010-12-03

T06:23:39-0500

Section 3.3 of the Atom Syndication format (http://www.atomenabled.org/developers/syndication/atom-format-spec.php#date.constructs)

DATE_COOKIE

D, d M Y H:i:s T

Fri, 03 Dec 2010

06:23:39 EST

HTTP Cookies (as defined at http://wp.netscape.com/newsref/std/cookie_spec.html)

DATE_ISO8601

Y-m-d\TH:i:sO

2010-12-03

T06:23:39-0500

ISO 8601 (as discussed at http://www.w3.org/TR/NOTE-datetime)

DATE_RFC822

D, d M Y H:i:s T

Fri, 03 Dec 2010

06:23:39 EST

Email messages (as defined in http://www.faqs.org/rfcs/rfc822.html)

DATE_RFC850

l, d-M-y H:i:s T

Friday, 03-Dec-10

06:23:39 EST

Usenet messages (as defined in http://www.faqs.org/rfcs/rfc850.html)

DATE_RFC1036

l, d-M-y H:i:s T

Friday, 03-Dec-10

06:23:39 EST

Usenet messages (as defined in http://www.faqs.org/rfcs/rfc1036.html)

DATE_RFC1123

D, d M Y H:i:s T

Fri, 03 Dec 2010

06:23:39 EST

As defined in http://www.faqs.org/rfcs/rfc1123.html

DATE_RFC2822

D, d M Y H:i:s O

Fri, 03 Dec 2010

06:23:39 -0500

E-mail messages (as defined in http://www.faqs.org/rfcs/rfc2822.html)

DATE_RSS

D, d M Y H:i:s T

Fri, 03 Dec 2010

06:23:39 EST

RSS feeds (as defined in http://blogs.law.harvard.edu/tech/rss)

DATE_W3C

Y-m-d\TH:i:sO

2010-12-03

T06:23:39-0500

Same as DATE_ISO8601


The formatting characters for date( ) are PHP-specific, but strftime( ) uses the C-library strftime( ) function. This may make strftime( ) more understandable to someone coming to PHP from another language, but it also makes its behavior slightly different on various platforms. Windows doesn't support as many strftime( ) formatting commands as most Unix-based systems. Also, strftime( ) expects each of its formatting characters to be preceded by a % (think printf( )), so it's easier to produce strings with lots of interpolated time and date values in them.

For example, at 12:49 P.M. on July 15, 2002, the code to print out:

It's after 12 pm on July 15, 2002

with strftime( ) looks like:

print strftime("It's after %I %P on %B %d, %Y");

With date( ) it looks like:

print "It's after ".date('h a').' on '.date('F d, Y');

Non-date-related characters in a format string are fine for strftime( ), because it looks for the % character to decide where to interpolate the appropriate time information. However, date( ) doesn't have such a delimiter, so about the only extras you can tuck into the formatting string are spaces and punctuation. If you pass strftime( )'s formatting string to date( ):

print date("It's after %I %P on %B%d, %Y");

you'd almost certainly not want what you'd get:

131'44 pmf31eMon, 15 Jul 2002 12:49:44 -0400 %1 %P o7 %742%15, %2002

To generate time parts with date( ) that are easy to interpolate, group all time and date parts from date( ) into one string, separating the different components with a delimiter that date( ) won't translate into anything and that isn't itself part of one of your substrings. Then, using explode( ) with that delimiter character, put each piece of the return value from date( ) in an array, which is easily interpolated in your output string. Figure does this, using a | character as a delimiter.

Using explode( ) with date( )

<?php
$ar = explode('|',date("h a|F d, Y"));
print "It's after $ar[0] on $ar[1]";
?>

See Also

Documentation on date( ) at http://www.php.net/date and strftime( ) at http://www.php.net/strftime; on Unix-based systems, man strftime for your system-specific strftime( ) options; on Windows, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strftime.2c_.wcsftime.asp for strftime( ) details.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows 

lexaz
lexaz 5 months, 3 weeks ago #
very helpful. thank you!