Home > Article > Backend Development > How to output the day of the week New Year's Day is in php
Output method: 1. Define an array containing weeks, with element values starting from Sunday; 2. Use the "date("w",strtotime("Year-1-1"))" statement to change the New Year's Day date Convert to a number representing the week; 3. Use the "echo array name [number]" statement to retrieve the corresponding week value from the array and output it.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php Output the day of the week that New Year's Day is
In PHP, you can use the date() function to determine the day of the week that the specified New Year's Day date is.
Implementation code:
<?php header('content-type:text/html;charset=utf-8'); $weekarray=array("日","一","二","三","四","五","六"); echo "2022年元旦是 星期".$weekarray[date("w",strtotime("2022-1-1"))]."<br>"; echo "2021年元旦是 星期".$weekarray[date("w",strtotime("2021-1-1"))]."<br>"; echo "2020年元旦是 星期".$weekarray[date("w",strtotime("2020-1-1"))]."<br>"; ?>
Analysis code:
Define an array containing the week: array("日","一" , "two", "three", "four", "five", "six")
Use date() to convert the date into a number representing the week;
Use the number as a subscript and retrieve the corresponding week value from the array.
Let’s take a look at the calendar and see if the output is correct:
#ok, the output is correct!
Recommended: "PHP Video Tutorial"
The above is the detailed content of How to output the day of the week New Year's Day is in php. For more information, please follow other related articles on the PHP Chinese website!