PHP date and time

PHPdate()Function used to format date or time

Timestamp, It is usually a sequence of characters that uniquely identifies a certain moment in time

Formatted date: The date() function will return a string generated by the parameter timestamp according to the specified format.

Syntax

date(format, timestamp)

A timestamp is a sequence of characters that represents the time and date when a specific event occurred


The format string can recognize the following format parameters String

format Required, specifies the format of the timestamp. Optional, specifies the timestamp. The default is the current time and date
##Parameter Description

timestamp

## Day  d  D ## j The day of the month, without leading zeros 1 to 31
format character Description ##Return value example
--- ---
The day of the month, a 2-digit number with leading zeros 01 to 31
The day of the week, text representation, 3 letters Mon to Sun
##
l (lower case letter of "L") Day of the week, complete text format Sunday to Saturday
 N ISO-8601 format number represents the day of the week (newly added in PHP 5.1.0) 1 (meaning Monday) to 7 (meaning Sunday) )
S The English suffix after the number of days in a month, 2 characters st, nd, rd or th. Can be used with j
w The day of the week, the number represents 0 (indicating Sunday) to 6 (indicating Saturday)
z The day of the year 0 to 365
Weekday --- ---

 W
ISO-8601 format The week number in the year, each week starts on Monday (newly added in PHP 4.1.0) For example: 42 (the 42nd week of the year)
moon --- ---

 F

#Month, complete text format, such as January or March January to December
m The month represented by numbers, with leading zeros 01 to 12
M The month represented by the three-letter abbreviation Jan to Dec
n The month represented by the number, without leading Zero 1 to 12
t The number of days in a given month 28 to 31
ISO-8601 format year number. This is the same value as Y , except that if the ISO week number (W) belongs to the previous or next year, that year is used. (New in PHP 5.1.0) ## Y  y
Year --- ---
L Whether it is a leap year If it is a leap year, it is 1, otherwise it is 0



# # o


Examples: 1999 or 2003

The year represented by a 4-digit complete number For example: 1999 or 2003
The year represented by a 2-digit number For example: 99 or 03
000 to 999 ## g hour, 12 hour format , no leading zeros 1 to 12
Time --- ---
a lower case The morning and afternoon values am or pm
## B Swatch Internet Standard Time
##
G hour, 24-hour format, no leading zeros 0 to 23
h Hour, 12-hour format, with leading zeros 01 to 12
H Hour, 24-hour format, with leading zeros Zero 00 to 23
i Number of minutes with leading zeros 00 to 59>
s seconds, with leading zeros 00 to 59>
## Time zone ## e Time zone identifier (newly added in PHP 5.1.0) For example: UTC, GMT, Atlantic/Azores I Whether it is daylight saving time If it is daylight saving time, it is 1, otherwise it is 0 O The number of hours difference from Greenwich Mean Time For example: +0200



u

Milliseconds (New in PHP 5.2.2 add). It should be noted that the date() function always returns 000000 because it only accepts integer parameters, and DateTime::format() only supports milliseconds.



## Example: 654321

--- ---
## # and Greenwich Mean Time (GMT), hours and minutes Separated by colons (new in PHP 5.1.3) For example: +02:00 Complete date/time  c


## The difference between P



 T



##The time zone where this machine is located

For example: EST, MDT ([Translator's Note] In complete text format under Windows, such as "Eastern Standard Time", the Chinese version will display "China Standard Time").


Z

The number of seconds of the time difference offset. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive.


-43200 to 43200

-- - ---
Date in ISO 8601 format (new in PHP 5) 2004-02-12T15 :19:21+00:00
r Date in RFC 822 format For example: Thu, 21 Dec 2000 16:01:07 +0200


U

The number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT)


See time()

PHP date() format date

The first required parameter of the date() function, format, specifies how to format the date/time.

Here are some available characters:

d - represents the day of the month (01 - 31)

m - represents the month (01 - 12)

Y - represents the year (four digits)

For a list of all characters available in the format parameter, please consult our PHP Date reference manual, date() function.

You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formats:

Example

"; echo date("Y.m.d") . "
"; echo date("Y-m-d"); ?>

Program running result:

2016/10/09
2016.10.09
2016-10-09


The following characters are commonly used for time:

·h - 12 with leading zero Hour Hour format

· i - minute with leading zero

· s - second with leading zero (00 -59)

· a - lowercase noon and afternoon (am or pm)


Example

The following example outputs the current time in the specified format:

The program execution result:

15:53:06pm

The PHP date() function will return the current time/date of the server


Get the time zone

If the time returned from the code is not the correct time, it is possible that your server is located in another country or is set to a different time zone .

So if you need an accurate time based on a specific location, you can set the time zone to use.

The following example sets the time zone to "Asia/Shanghai", and then outputs the current time in the specified format:

The program execution result:

The current time is 03:54:49pm


##Created date via PHP mktime()
## The optional timestamp parameter in the #date() function specifies the timestamp. If you do not specify a timestamp, the current date and time will be used (as in the example above).

The mktime() function returns the Unix timestamp of a date. A Unix timestamp contains the number of seconds between the Unix epoch (January 1, 1970 00:00:00 GMT) and the specified time.

grammar

mktime(hour,minute,second,month,day,year)

The following example A series of parameters in the mktime() function will be used to create the date and time

Program running results:

The creation date is 2016-06-10 09:12:31am


Create dates from strings via PHP strtotime()

The PHP strtotime() function is used to convert human Convert the read string to Unix time.

Syntax

##strtotime(time,now)

The following example creates date and time through strtotime() function:

Program running result:

The creation date is 2016-04-15 10:38:00pm


PHP is very smart about converting strings to dates, so you can use a variety of values:

Example

"; $d=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $d) . "
"; $d=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $d) . "
"; ?>

Program running result:

2016-10-10 12 :00:00am

2016-10-15 12:00:00am
2017-01-09 04:01:09pm

Note: strtotime() It's not perfect, so remember to check the strings you put in it.


Example

"; $startdate = strtotime("+1 week", $startdate); } ?>

Program running result:

Oct 15

Oct 22
Oct 29
Nov 05
Nov 12


## Example
Use the strtotime() function to obtain the timestamp of the English format date and time string

"; //当前时间的时间戳 echo "输出时间:".date("Y-m-d H:i:s",strtotime("now")),"
"; //输出当前时间 echo strtotime("24 May 2016"),"
"; //输出指点时间的时间戳 echo "输出时间:".date("Y-m-d H:i:s",strtotime("24 May 2016")),"
"; //输入指定的日期 ?>

Program execution result:

1476000300
Output time: 2016-10-09 16:05:00

1464019200
Output time: 2016-05-24 00:00:00


Example

Realizing the countdown function

$sub 小时"; echo "

"; echo"离元旦放假时间还有 $sub1 天"; ?>

Program running result:

There are 8564 hours left before the National Day holiday

There are -273 days left before the New Year’s Day holiday

##Complete PHP Date Reference Manual


For a complete reference manual for all date functions, please visit our Complete PHP Date Reference Manual.

This reference manual provides a brief description and application examples of each function!


Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!