PHP has a very powerful system function, the date () function. Clever use of him can be achieved by displaying any time we need.
Today, for example, there is a need to judge today is the first few weeks of this month, here is not to discuss the question and whether it is meaningful, the following concrete see how to use PHP to implement this function.
This function mainly uses the date () function of the W J two parameters, the date () function parameters are many.
The PHP date () parameter description explains the two parameters of W J as follows:
W represents day of the week, number represents 0 (for Sunday) to 6 (for Saturday)
The day of the J month, the numbers indicate that from 1 to 31 the specific algorithm that uses PHP to judge the week of the month today is: the use of the date (that is, the number) and the total days of the week (7 days) of the relationship, the borrowing Ceil () function directly to draw today is the first few weeks of this month.
The Ceil () function is used to calculate the smallest integer greater than the specified number (floating-point, float number).
For example: Assuming that 3rd is Thursday, then the value of Ceil (3/7) will be 1, indicating that the day is the first Thursday of the month. The next Thursday calculation is Ceil (10/7), which has a value of 2, indicating that 10th is the second Thursday. Other, and so on. Based on this algorithm, you can determine the calculation formula for calculating the number of weeks in the current month is set to: ceil (Date/7).
Let's look at a specific example:
/*
function: Calculate the number of weeks of today's month
*/
Header (' content-type:text/html; Charset=utf-8 ');
$wk _day=date (' W '); Get today is the day of the week
$date _now=date (' J '); What's the date today?
$wkday _ar=array (' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' VI '); Normalize the expression of the Sunday
$cal _result=ceil ($date _NOW/7); Calculation is the first few weeks
$str =date ("Y-year N-month J-Day"). "Week". $wkday _ar[$wk _day]. "-This month". $cal _result. $wkday _ar[$wk _day];
Echo $str;
?>
The results of this run are as follows:
May 21, 2013 Tuesday-3rd Tuesday of the Month