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
##Parameter | Description |
Required, specifies the format of the timestamp. | |
timestamp | Optional, specifies the timestamp. The default is the current time and date
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 | ## j |
1 to 31 |
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 |
Year | --- | --- |
L | Whether it is a leap year | If it is a leap year, it is 1, otherwise it is 0 |
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 |
Time | --- | --- |
a | lower case The morning and afternoon values | am or pm |
## B | Swatch Internet Standard Time | |
## g | hour, 12 hour format , no leading zeros | 1 to 12 |
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> |
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. | ||||||||||||||||||||||
--- | --- | ## e | |||||||||||||||||||||
For example: UTC, GMT, Atlantic/Azores | I | ||||||||||||||||||||||
If it is daylight saving time, it is 1, otherwise it is 0 | O | ||||||||||||||||||||||
For example: +0200 | ##
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") . " Program running result:
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:
☆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:
##Created date via PHP mktime() 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:
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) . "Program running result: 2016-10-10 12 :00:00am 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
## Example "; //当前时间的时间戳 echo "输出时间:".date("Y-m-d H:i:s",strtotime("now"))," Program execution result: 1476000300Output time: 2016-10-09 16:05:00 Example Realizing the countdown function $sub 小时"; echo " Program running result: There are 8564 hours left before the National Day holiday##Complete PHP Date Reference Manual
This reference manual provides a brief description and application examples of each function!
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
|