Home  >  Article  >  Backend Development  >  How to use date in php to get the day

How to use date in php to get the day

青灯夜游
青灯夜游Original
2022-05-10 20:46:302207browse

php date() function can obtain the year, month or day of the week by passing in different formatting characters: 1. When the character is "z", you can obtain the day of the year Day; 2. When the character is "d" or "j", the day of the month can be obtained; 3. When the character is "D", "N" or "w", the day of the week can be obtained.

How to use date in php to get the day

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, date() The function can format a timestamp and obtain the day of the week, month, or year.

Syntax:

date("格式化字符"[,时间戳])

The second parameter can be omitted, then it is the current date and time.

There are three formatting characters for getting the day of the year:

  • The day of the year

    • z: Range 0 to 365

  • The day of the month

    • d: With leading 2 digits of zero, range 01 to 31

    • j: no leading zero, range 1 to 31

  • Day of the week

    • D: text representation, 3 letters, range Mon to Sun

    • N: ISO-8601 format number Indicates the day of the week (newly added in PHP 5.1.0), ranging from 1 (indicating Monday) to 7 (indicating Sunday)

    • w: numerical representation, range 0 ( Indicates Sunday) to 6 (indicates Saturday)

Example 1:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
echo "今天是一年的第 ".date("z"). " 天<br>";
?>

How to use date in php to get the day

##Example 2:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
echo "今天是5月的第 ".date("d"). " 天<br>";
echo "今天是5月的第 ".date("j"). " 天<br>";
?>

How to use date in php to get the day

How to use date in php to get the day##Example 3:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
echo "今天是一周的第 ".date("D"). " 天<br>";
echo "今天是一周的第 ".date("N"). " 天<br>";
echo "今天是一周的第 ".date("w"). " 天<br>";
?>

How to use date in php to get the day Recommended study: "

PHP video tutorial

The above is the detailed content of How to use date in php to get the day. 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