Home > Backend Development > PHP Tutorial > PHP Programming Tips: Calculate the Day of the Week for Any Date

PHP Programming Tips: Calculate the Day of the Week for Any Date

WBOY
Release: 2024-02-29 18:14:01
Original
972 people have browsed it

PHP Programming Tips: Calculate the Day of the Week for Any Date

PHP Programming Tips: Calculate the day of the week for any date

In PHP programming, you often encounter situations where you need to calculate the day of the week for a certain date. Although PHP provides date and time functions, sometimes we need to write our own code to implement this function. This article will introduce how to use PHP to write a function to calculate the day of the week for any date, and attach specific code examples.

First of all, we need to understand a basic calculation formula: Zeiller's formula. Zeiler's formula is an algorithm for calculating the day of the week for any date. The formula is as follows:

[ h = (q [13(m 1]/5) K [K/4] [J/ 4] - 2J) % 7 ]

Where, h is the day of the week (0 is Sunday, 1 is Monday, and so on), q is the date (1 to 31), m is the month (3 is March, 4 is April, and so on, but January and February are calculated as the 13th and 14th months of the previous year), K is the single digit of the year, and J is the tens digit of the year.

With this formula as a basis, we can write a PHP function to calculate the day of the week for any date. The following is the sample code:

function getWeekday($year, $month, $day) {
    if ($month < 3) {
        $year--;
        $month += 12;
    }
    
    $K = $year % 100;
    $J = floor($year / 100);
    
    $h = ($day + floor((13*($month+1))/5) + $K + floor($K/4) + floor($J/4) - 2*$J) % 7;
    
    return $h;
}

// 以2022年5月20日为例
$year = 2022;
$month = 5;
$day = 20;
$weekday = getWeekday($year, $month, $day);

$weekday_map = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

echo "{$year}年{$month}月{$day}日是{$weekday_map[$weekday]}";
Copy after login

In the above sample code, we define a function called getWeekday, which accepts three parameters: year, month, and day, and then calculates the day of the week according to Zeiler's formula, and Return the corresponding number. Finally, we use a mapped array containing the days of the week to convert the number into a specific day of the week name, and output the result.

Through this function, we can easily calculate the day of the week for any date, providing a flexible solution for processing date and time-related needs in PHP programming. I hope this article can help readers who are learning PHP programming.

The above is the detailed content of PHP Programming Tips: Calculate the Day of the Week for Any Date. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template