In web development, we often need to calculate what day of the week a certain date is. PHP provides a built-in function date()
that can help us complete this task. This article will introduce how to use the date()
function in PHP to calculate the day of the week for a certain date.
In PHP, the date()
function can get the current date and time and format them into a specified string. Common formatting options include date, time, day of week, etc. The specific usage is as follows:
string date ( string $format [, int $timestamp = time() ] )
Among them, the format
parameter indicates the string format that needs to be formatted, and the timestamp
parameter is optional and indicates the time that needs to be formatted. stamp. If this parameter is not passed, it defaults to the current timestamp. The return value is a formatted date string. For example:
echo date('Y-m-d H:i:s'); //输出当前时间的标准格式
The output result is in a format similar to the following:
2021-05-11 16:21:30
date()
The function can also pass some special formats option to output the day of the week for a certain date. The following are commonly used day-of-week formatting options:
l
(lowercase L) - the full name of the day of the week, for example: Sunday, Monday, etc. N
- The day of the week represented by a number, for example: 1 means Monday, 2 means Tuesday, etc. D
- Abbreviated day of the week name, for example: Sun, Mon, etc. We can use the date()
function in conjunction with the above formatting options to calculate the day of the week a certain date is. For example, the following code can output what day of the week today is:
echo date('l'); //输出今天是星期几的完整名称
The output result is:
Tuesday
Similarly, if we want to output the numerical representation of what day of the week today is, we can use N
Format options:
echo date('N'); //输出今天是星期几的数字形式
The output result is:
2
In addition to the above example, we can also usedate()
Function to calculate the day of the week for any date. The specific steps are as follows:
date()
function and use the l
or N
formatting option to Outputs the day of the week the target date is. The following is a sample code to calculate the day of the week for any date:
//要计算的目标日期 $date = '2021-05-10'; //将目标日期转换为时间戳 $timestamp = strtotime($date); //使用date函数输出星期几 echo date('l', $timestamp); //完整名称形式,输出Monday echo date('N', $timestamp); //数字形式,输出1 echo date('D', $timestamp); //简写形式,输出Mon
Since the input date is '2021-05-10', which is Monday, the above code outputs the result They should all be Monday
, 1
and Mon
.
In order to facilitate future calls, we can encapsulate the above code into a function. The following is a sample code:
function getDayOfWeek($date) { $timestamp = strtotime($date); return date('l', $timestamp); } echo getDayOfWeek('2021-05-10'); //输出 Monday
Using this function, we can more conveniently calculate the day of the week for a certain date. Pass a date as a parameter to the function and the function will return the full name of the day of the week for that date.
In PHP, we can use the date()
function to calculate the day of the week for any date. Just convert the date to a timestamp and use the l
or N
formatting options to output the day of the week for the target date. If you need to perform this calculation frequently, you can encapsulate the code into a function to improve the reusability and readability of the code.
The above is the detailed content of How to use date() function in php to calculate the day of the week for a certain date. For more information, please follow other related articles on the PHP Chinese website!