Recipe 3.7. Finding the Day in a Week, Month, or Year


3.7.1. Problem

You want to know the day or week of the year, the day of the week, or the day of the month. For example, you want to print a special message every Monday, or on the first of every month.

3.7.2. Solution

Use the appropriate arguments to date( ) or strftime( ) , as shown in Example 3-17.

Finding days of the week, month, and year

<?php print strftime("Today is day %d of the month and %j of the year."); print 'Today is day '.date('d').' of the month and '.date('z').' of the year.'; ?>

3.7.3. Discussion

The two functions date( ) and strftime( ) don't behave identically. Days of the year start with 0 for date( ), but with 1 for strftime( ). Table 3-4 contains all the day and week number format characters date( ) and strftime( ) understand.

Table 3-4. Day and week number format characters

Type

strftime( )

date( )

Description

Range

Windows

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


To print out something only on Mondays, use the w formatting character with date( ) or the %w string with strftime( ), as in Example 3-18.

Checking for the day of the week

<?php if (1 == date('w')) {     print "Welcome to the beginning of your work week."; } if (1 == strftime('%w')) {     print "Only 4 more days until the weekend!"; }

There are different ways to calculate week numbers and days in a week, so take care to choose the appropriate one. The ISO standard (ISO 8601), says that weeks begin on Mondays and that the days in the week are numbered 1 (Monday) through 7 (Sunday). Week 1 in a year is the first week in a year with a Thursday in that year. This means the first week in a year is the first week with a majority of its days in that year. These week numbers range from 01 to 53.

Other week number standards range from 00 to 53, with days in a year's week 53 potentially overlapping with days in the following year's week 00.

As long as you're consistent within your programs, you shouldn't run into any trouble, but be careful when interfacing with other PHP programs or your database. For example, MySQL's DAYOFWEEK( ) function treats Sunday as the first day of the week, but numbers the days 1 to 7, which is the ODBC standard. Its WEEKDAY( ) function, however, treats Monday as the first day of the week and numbers the days from 0 to 6. Its WEEK( ) function lets you choose whether weeks should start on Sunday or Monday, but it's incompatible with the ISO standard.

3.7.4. See Also

Documentation on date( ) at http://www.php.net/date and strftime( ) at http://www.php.net/strftime; MySQL's DAYOFWEEK( ), WEEKDAY( ), and WEEK( ) functions are documented at http://www.mysql.com/doc/D/a/Date_and_time_functions.html .




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net