Home  >  Article  >  Backend Development  >  How to use date() function in php to calculate the day of the week for a certain date

How to use date() function in php to calculate the day of the week for a certain date

PHPz
PHPzOriginal
2023-04-24 09:12:451240browse

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.

date() function

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

Calculate the day of the week

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

Calculate the day of the week for any date

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:

  1. First, the target date needs to be converted into a timestamp. A timestamp is a number that represents time. It represents the number of seconds that have passed since 0:00:00 on January 1, 1970.
  2. Then, pass the timestamp as the second parameter to the 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.

Encapsulated as a function

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.

Summary

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!

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