There is a very powerful system function date() function in PHP. Clever use of it can display any time we need.
For example, today I encountered a need to determine which day of the month it is today. I will not discuss whether there are any problems and whether this kind of language is meaningful. Let's look at how to use PHP to implement this function.
This function mainly uses the w j parameters of the date() function. The date() function has many parameters.
PHP date() parameter description The explanation of the two parameters w j is as follows:
w represents the day of the week, and the number represents 0 (representing Sunday) to 6 (representing Saturday)
j The day of the month, the number represents from 1 to 31. The specific algorithm for using PHP to determine which day of the week today is this month is: using the relationship between the date (that is, the number) and the total number of days in the week (7 days), borrow the ceil() function to directly determine the day of the week today is this month.
The ceil() function is used to calculate the smallest integer greater than a specified number (float number).
For example: Suppose the 3rd of a certain month is a Thursday, then the value of ceil(3/7) will be 1, which indicates that this day is the first Thursday of the month. The calculation formula for the next Thursday is ceil(10/7), whose value is 2, indicating that the 10th is the second Thursday. Others can be deduced in turn. According to this algorithm, it can be determined that the calculation formula for calculating which day of the week today is in the month is set to: ceil (date/7).
Let’s look at a specific example:
/*
功能: 计算今日是当月的第几个星期几
*/
header('content-Type: text/html; charset=utf-8');
$wk_day=date('w'); //得到今天是星期几
$date_now=date('j'); //得到今天是几号
$wkday_ar=array('日','一','二','三','四','五','六'); //规范化周日的表达
$cal_result=ceil($date_now/7); //计算是第几个星期几
$str=date("Y年n月j日")." 星期".$wkday_ar[$wk_day]." - 本月的第 ".$cal_result." 个星期".$wkday_ar[$wk_day];
echo $str;
?>The results of this run are as follows:
Tuesday, May 21, 2013 - the 3rd Tuesday of the month
This article was originally published on php Chinese website. Please indicate the source when reprinting. Thank you for your respect! ::(insidious)