Home  >  Article  >  Backend Development  >  How to determine what day of the week it is using php

How to determine what day of the week it is using php

silencement
silencementOriginal
2019-09-25 15:15:444003browse

How to determine what day of the week it is using php

There is a very powerful system function date() function in php. We can use it to display any time we need. For example, today I encountered a need to determine which day of the month today is. Let’s see 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. If you want to know more about this function, please refer to the manual.

PHP date() parameter description

The explanation of the two parameters w j is as follows:

w 表示星期中的第几天,数字表示 0(表示星期天)到 6(表示星期六)
j 月份中的第几天,数字表示从 1 到 31

A specific algorithm for using PHP to determine which day of the week today is in this month Yes:

Using the relationship between the date (that is, the number) and the total number of days in the week (7 days), use the ceil() function to directly get the day of the week today is. The ceil() function is used to calculate the smallest integer greater than a specified number (float number). For example:

Assume that 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 follow suit. 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).

<!--?php
header(&#39;content-Type: text/html; charset=utf-8&#39;);
$wk_day=date(&#39;w&#39;);   //得到今天是星期几
$date_now=date(&#39;j&#39;); //得到今天是几号
$wkday_ar=array(&#39;日&#39;,&#39;一&#39;,&#39;二&#39;,&#39;三&#39;,&#39;四&#39;,&#39;五&#39;,&#39;六&#39;); //规范化周日的表达
$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 this month.

The above is the detailed content of How to determine what day of the week it is using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn