php date() is a function that gets the time and date. PHP can display the server date and time through the date() function. Next, let’s take a look at the details of the PHP data function.
php date() function detailed explanation
1, year-month-day
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9668')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9668> echo date('Y-m-j');2007-02-6 echo date('y-n-j');07-2-6 </td> </tr> </table>
Uppercase Y represents four digits of the year, while lowercase y represents the two digits of the year;
Lowercase m represents the number of the month (with leading), while lowercase n represents the number of the month without the leading.
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3173')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3173> echo date('Y-M-j');2007-Feb-6 echo date('Y-m-d');2007-02-06 </td> </tr> </table>
Capital M represents the 3 abbreviation characters of the month, while lowercase m represents the number of the month (with leading 0);
There is no uppercase J, only lowercase j represents the date of the month, without leading o ; If you need a leading month, use lowercase d.
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5128')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5128> echo date('Y-M-j');2007-Feb-6 echo date('Y-F-jS');2007-February-6th </td> </tr> </table>
Capital M represents the 3 abbreviated characters of the month, while capital F represents the full English character of the month. (No lowercase f)
Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number.
Summary:
can represent the year with uppercase Y and lowercase y;
can represent the month with uppercase F, uppercase M, lowercase m and lowercase n (two ways to represent characters and numbers respectively) );
can use lowercase d and lowercase j to represent the day, and uppercase S represents the suffix of the date.
2, hours: minutes: seconds
By default, the time displayed by PHP interpretation is "Greenwich Mean Time", which is 8 hours different from our local time.
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9299')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9299> echo date('g:i:s a');5:56:57 am echo date('h:i:s A');05:56:57 AM </td> </tr> </table>
The lowercase g indicates the 12-hour format without leading 0, while the lowercase h indicates the 12-hour format with leading 0.
When using the 12-hour clock, it is necessary to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1159')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1159> echo date('G:i:s');14:02:26 </td> </tr> </table>
Capital G represents the number of hours in the 24-hour format, but without leading; use uppercase H to represent the number of hours in the 24-hour format with leading
Summary:
The letter g represents The hour does not have a leading character, and the letter h represents the hour with a leading character;
lowercase g and h represent the 12-hour format, and uppercase G and H represent the 24-hour format.
3, leap year, week, day
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1723')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1723> echo date('L');今年是否闰年:0 echo date('l');今天是:Tuesday echo date('D');今天是:Tue </td> </tr> </table>
Uppercase L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
Lowercase l indicates the day of the week. The full English version (Tuesday);
uses a capital D to represent the 3-character abbreviation of the day of the week (Tue).
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8851')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8851> echo date('w');今天星期:2 echo date('W');本周是全年中的第 06 周 小写w表示星期几,数字形式表示大写W表示一年中的星期数 echo date('t');本月是 28 天 echo date('z');今天是今年的第 36 天 </td> </tr> </table>
Lowercase t represents the number of days in the current month
Lowercase z represents the day of the year today is
4, other time zone issues in the date function
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2354')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2354> echo date('T');UTC大写T表示服务器的时间区域设置 1echo date('I');0大写I表示判断当前是否为夏令时,为真返回1,否则为0 echo date('U');1170769424大写U表示从1970年1月1日到现在的总秒数,就是Unix时间纪元的UNIX时间戳。 echo date('c');2007-02-06T14:24:43 00:00小写c表示ISO8601日期,日期格式为YYYY-MM-DD,用字母T来间隔日期和时间,时间格式为HH:MM:SS,时区使用格林威治标准时间(GMT)的偏差来表示。 echo date('r');Tue, 06 Feb 2007 14:25:52 0000小写r表示RFC822日期。 </td> </tr> </table>
date("Y-m-d h:i:s") The total server time differs by several hours
The solution is as follows:
1. Use date_default_timezone_set() in the header of the page to set My default time zone is Beijing time
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1492')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1492>date_default_timezone_set('PRC'); echo date('Y-m-d H:i:s'); </td> </tr> </table>
The time is the same as the current time of the server!!
2. Modify php.ini.
Open php5.ini and search for date.timezone. Remove the semicolon = followed by XXX and restart the http service
(such as apache2 or iis, etc.).
About XXX, the available values in mainland China are: Asia/Chongqing, Asia/Shanghai,
Asia/Urumqi (in order Chongqing, Shanghai, Urumqi) Available values in Hong Kong and Taiwan: Asia/Macao,
Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, Taipei in order) and Singapore:
Asia/Singapore foreigners seem to have missed Beijing. Other available values are: Etc/GMT- 8. Singapore,
Hongkong, PRC, what is PRC? PRC is the People's Republic of China -_- The above are the regions under GMT+8 compiled from
in the official PHP manual, there may be some omissions.
date function month and day with 0 problems
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5683')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5683> 一、带零 echo date('Y-m-d');2012-08-08 二、不带零 echo date('Y-n-j');2012-8-8 </td> </tr> </table></td> </tr> </table>
Related recommendations:
PHP date and time, PHP date time
Usage of date and time functions in PHP
The above is the detailed content of Share the method of outputting time by date() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools