Time and date in PHP

Source: Internet
Author: User
Tags time and date

A, date () function

string Date (string $format [, int $timestamp])
Function: Formats the timestamp as an easy-to-read time and date.

Parameters Description
Format Necessary. Specifies the format of the timestamp.
Timestamp Optional. Time stamp is specified. The default is the current time and date.
1. Date of acquisition (1), year
format Formats Description Example
L Whether it is a leap year If the leap year is 1, otherwise 0
O ISO-8601 format year number. This is the same value as Y, except if the day of the Week (W) of the ISO belongs to the previous year or the next year. (PHP 5.1.0 new addition) examples:1999 or 2003
Y 4-digit year in full representation Example: 1999 or 2003
Y Year 2-digit representation Example: 99 or 03
(2), month
format Formats Description Example
F month, full text format, such as January or March January to December
M A number represents the month, with a leading zero 01 to 12
M Three letter abbreviation for month Jan to Dec
N The number represents the month, without leading zeros 1 to 12
T The number of days that a given month should be 28 to 31
(3), Japan
format Formats Description Example
D The day ordinal of a month with a leading zero 2-digit number 01 to 31
D Day of the week, text representation, 3 letters Mon to Sun
J The day ordinal of the month without leading zeros 1 to 31
L (Lowercase letter of "L") Day of the week, full text format Sunday to Saturday
N ISO-8601 format number represents the day of the week (PHP 5.1.0 new Plus) 1 (for Monday) to 7 (= Sunday)
S English suffix, 2 characters, after the number of days per month st,nd,rd or th. Can be used with J.
W Day of the week, numbers indicate 0 (for Sunday) to 6 (= Saturday)
Z The day ordinal of the year 0 to 365
(4), week
format Formats Description Example
W Week of the ISO-8601 format year, starting from Monday (PHP 4.1.0 New) Example: 42 (42nd Week of the year)
2. Acquisition time (1), Time td> hours, 12-hour format, no leading 0
format format description example
a< /td> lowercase morning and afternoon values am or PM
A uppercase morning and afternoon values am or PM
B Swatch Internet standards 000 to 999
g 1 to
G hours, 24-hour format, no preamble 0 0 to 23
h hours, 12-hour format, with leading 0 01 to +
h Small 24-hour format with leading 0 00 to all
i number of minutes with leading zeros 00 to 59>
S number of seconds, with leading 0 00 to 59>
u ms (PHP 5.2). 2 new Plus). It is important to note that the date () function always returns 000000 because it accepts only the integer parameter, and Datetime::format () supports milliseconds. Example: 654321
(2), Time zone
format Formats Description Example
E Time zone ID (PHP 5.1.0 new addition) Example: Utc,gmt,atlantic/azores
I Whether it is daylight saving time If daylight saving time is 1, otherwise 0
O Hours of difference from GMT Example: +0200
P Difference from GMT (GMT), between hours and minutes colon separated (PHP 5.1.3 New) Example: +02:00
T The time zone in which this machine resides For example: EST,MDT ("translator note" in the full text format under Windows, such as "Eastern Standard Time", the Chinese version will show "China normal times").
Z The number of seconds of the slack offset. The time zone offset in the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. 43200 to 43200
Second, mktime () function

mktime (hour,minute,second,month,day,year)
Function: Returns the UNIX timestamp according to the given parameters.
The timestamp is a long integer containing the number of seconds from the Unix era (January 1 1970 00:00:00 GMT) to a given time.

Three, strtotime () function

int Strtotime (string $time [, int $now = time ()])
function: Resolves a datetime description of any English text to a Unix timestamp
Time: DateTime string. The correct format is described in the date and time format.
Now: the timestamp used to calculate the return value.

Note: Modify the time zone to get local time

    1. Modify the date.timezone field in the php.ini file
      Remove the preceding semicolon, add the PRC (Beijing time), and restart the Appache
    2. Initialize time in code date_default_timezone_set (XXX)
    3. In addition: About XXX, the values available in mainland China are: asia/chongqing, Asia/shanghai, Asia/urumqi (Chongqing, Shanghai, Urumqi, respectively)
      Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macao, Hong Kong, Taipei, respectively). and Singapore: Asia/singapore.
      Other available values are: Etc/gmt-8, Singapore, Hongkong, PRC
Iv. a simple calendar that I did after I finished this study:
<html><head><!--<meta http-equiv= "Refresh" content= "3" >--></head><body><table cellpadding="Ten"><tr><TD>Sun</td><TD>Mon</td><TD>Tue</td><TD>Wed</td><TD>Thu</td><TD>Fri</td><TD>Sat</td></tr><?php#显示一张日历$SATNUM=0;$MONTH=date ("M");Echo "<tr>"; for($i=0;$i<date ("W", Strtotime ("2015/$MONTH/01"));$i++){Echo "<td></td>";} for($j=0,$i=date ("W", Strtotime ("2015/$MONTH/01"));$i<=6;$i++,$j++){$tmp= on+$j;$GLOBALS[' Satnum ']=$tmp;Echo "<td> $tmp </td>";}Echo "</tr>"; for($j=0;$j<4;$j++){Echo "<tr>"; for($i=0;$i<7;$i++){$tmp=++$GLOBALS[' Satnum '];if($tmp==date ("D"))Echo "<td bgcolor=blue> $tmp </td>";ElseIf(! ($tmp>date ("T")))Echo "<td> $tmp </td>"; }Echo "</tr>";}?><?php #确定星期几$week="";Switch(Date ("W")){ Case 0:$week="Sunday"; Break; Case 1:$week="Monday"; Break; Case 2:$week="Tuesday"; Break; Case 3:$week="Wednesday"; Break; Case 4:$week="Thursday"; Break; Case 5:$week="Friday"; Break; Case 6:$week="Saturday"; Break;}#动态显示当前时间Echo "The time is now:". Date ("Y year M D Day $week H I min s second");Echo "<br/>";?></body></html>

Copyright NOTICE: This article for Lshare original article, need to reprint please contact me, have questions welcome comments or private messages.

Time and date in PHP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.