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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles