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

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

不言
不言Original
2018-07-17 10:59:262728browse

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>

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>

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>

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>

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>

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>

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>

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>

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>

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>

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:
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