PHP date and time
PHP date() 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)
##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 <?php echo date("Y/m/d") . "<br>"; echo date("Y.m.d") . "<br>"; 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: <?php echo date ("H:i:sa"); ?> 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: <?php header("Content-type:text/html;charset=utf-8"); //设置编码 date_default_timezone_set("Asia/Shanghai"); echo "当前时间是 " . date("h:i:sa"); ?> 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: 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: 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 <?php $d=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("next Saturday"); echo date("Y-m-d h:i:sa", $d) . "<br>"; $d=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $d) . "<br>"; ?>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 <?php $startdate = strtotime("Saturday"); $enddate = strtotime("+5 weeks",$startdate); while ($startdate < $enddate) { echo date("M d", $startdate),"<br>"; $startdate = strtotime("+1 week", $startdate); } ?> Program running result: Oct 15 ## Example <?php header("Content-type:text/html;charset=utf-8"); //设置编码 echo strtotime("now"),"<br>"; //当前时间的时间戳 echo "输出时间:".date("Y-m-d H:i:s",strtotime("now")),"<br>"; //输出当前时间 echo strtotime("24 May 2016"),"<br>"; //输出指点时间的时间戳 echo "输出时间:".date("Y-m-d H:i:s",strtotime("24 May 2016")),"<br>"; //输入指定的日期 ?> Program execution result: 1476000300Output time: 2016-10-09 16:05:00 Example Realizing the countdown function <?php header("Content-type:text/html;charset=utf-8"); //设置编码 $time=strtotime(date("Y-m-d H:i:s")); //当前的系统时间 $time1=strtotime("2017-10-1 12:00:00"); //国庆节的放假时间 $time2=strtotime("2017-1-1"); //元旦的放假时间 $sub=ceil(($time1-$time)/3600); //(60秒*60分)/小时 $sub1=ceil(($time2-$time1)/86400); //(60秒*60分*24小时)/天 echo "离国庆放假时间还有 <strong>$sub</strong> 小时"; echo "<p>"; echo"离元旦放假时间还有 <strong>$sub1</strong> 天"; ?> 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
|