Home > Backend Development > PHP Problem > How to output the day of the week New Year's Day is in php

How to output the day of the week New Year's Day is in php

青灯夜游
Release: 2023-03-15 16:58:02
Original
1260 people have browsed it

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.

How to output the day of the week New Year's Day is in php

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(&#39;content-type:text/html;charset=utf-8&#39;);
$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>";
?>
Copy after login

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.

How to output the day of the week New Years Day is in php

Let’s take a look at the calendar and see if the output is correct:

How to output the day of the week New Years Day is in php

How to output the day of the week New Years Day is in php

How to output the day of the week New Years Day is in php

#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!

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