Home Backend Development PHP Tutorial Share the method of outputting time by date() function in PHP

Share the method of outputting time by date() function in PHP

Jul 17, 2018 am 10:59 AM

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(&#39;copy9668&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9668>
echo date(&#39;Y-m-j&#39;);2007-02-6
echo date(&#39;y-n-j&#39;);07-2-6
</td> </tr> </table>
Copy after login

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(&#39;copy3173&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3173>
echo date(&#39;Y-M-j&#39;);2007-Feb-6
echo date(&#39;Y-m-d&#39;);2007-02-06
</td> </tr> </table>
Copy after login

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(&#39;copy5128&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5128>
echo date(&#39;Y-M-j&#39;);2007-Feb-6
echo date(&#39;Y-F-jS&#39;);2007-February-6th
</td> </tr> </table>
Copy after login

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(&#39;copy9299&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9299>
echo date(&#39;g:i:s a&#39;);5:56:57 am
echo date(&#39;h:i:s A&#39;);05:56:57 AM
</td> </tr> </table>
Copy after login

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(&#39;copy1159&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1159>
echo date(&#39;G:i:s&#39;);14:02:26
</td> </tr> </table>
Copy after login

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(&#39;copy1723&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1723>
echo date(&#39;L&#39;);今年是否闰年:0
echo date(&#39;l&#39;);今天是:Tuesday
echo date(&#39;D&#39;);今天是:Tue
</td> </tr> </table>
Copy after login

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(&#39;copy8851&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8851>
echo date(&#39;w&#39;);今天星期:2
echo date(&#39;W&#39;);本周是全年中的第 06 周
小写w表示星期几,数字形式表示大写W表示一年中的星期数
echo date(&#39;t&#39;);本月是 28 天
echo date(&#39;z&#39;);今天是今年的第 36 天
</td> </tr> </table>
Copy after login

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(&#39;copy2354&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2354>
echo date(&#39;T&#39;);UTC大写T表示服务器的时间区域设置
1echo date(&#39;I&#39;);0大写I表示判断当前是否为夏令时,为真返回1,否则为0
echo date(&#39;U&#39;);1170769424大写U表示从1970年1月1日到现在的总秒数,就是Unix时间纪元的UNIX时间戳。
echo date(&#39;c&#39;);2007-02-06T14:24:43 00:00小写c表示ISO8601日期,日期格式为YYYY-MM-DD,用字母T来间隔日期和时间,时间格式为HH:MM:SS,时区使用格林威治标准时间(GMT)的偏差来表示。
echo date(&#39;r&#39;);Tue, 06 Feb 2007 14:25:52 0000小写r表示RFC822日期。
</td> </tr> </table>
Copy after login

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(&#39;copy1492&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1492>date_default_timezone_set(&#39;PRC&#39;);
echo date(&#39;Y-m-d H:i:s&#39;);
</td> </tr> </table>
Copy after login

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(&#39;copy5683&#39;)">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5683>
一、带零
echo date(&#39;Y-m-d&#39;);2012-08-08
二、不带零
echo date(&#39;Y-n-j&#39;);2012-8-8
</td> </tr> </table></td> </tr> </table>
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles