Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
During website development, we often need to process the date and time. in PHP, many date and time functions are provided to facilitate PHP developers to calculate and convert the date and time formats. Therefore, it is necessary to master the PHP date function and convert the format between the PHP date function and the Mysql database date... syntaxHighlighter. all (); during website development, we often need to process the date and time. in PHP, many date and time functions are provided to facilitate PHP developers to calculate and convert the date and time. Therefore, it is necessary to master the PHP date function and lay the foundation for processing the format conversion between the PHP date function and the Mysql database date. Here we will introduce the basics of PHP in PHP: date and Unix timestamp functions of date functions and conversion methods of date formats between them.
PHP date and time zone settings
Before enabling the PHP date function, make sure that the set time zone is correct. Otherwise, the displayed date may be different. You only need to set date. timezone as the corresponding time zone in the PHP. INI configuration file. If you are not operating PHP. INI configuration file permission. you can use the PHP time zone function date_default_timezone_get (void) to obtain the time zone of the current PHP runtime environment, and then use the date_default_timezone_set (string $ timezone_identifier) function to set the corresponding time, for more time zones supported by PHP, see http://www.php.net/manual/en/timezones.php.
PHP format Date function
Prototype string date (string $ format [, int $ timestamp])
$ Format-PHP date function date format code
$ Timestamp-Unix timestamp. the default value is the current timestamp (optional)
Format character ---- description --- return value description
D -- the day of the month, returns two digits -- 01 to 31
J -- the day of the month, 1 to 9 without zero -- 1 to 31
S-English suffix of the number of days in a month-2 characters (st, nd, rd, or th. Can be used with j
Z -- the day of the year -- 0 to 366
L -- day of the week -- Sunday to Saturday
D: The day of the week in English. the text indicates the day of the week from Mon to Sun.
N -- represent the day of the week in numbers (PHP 5.1.0 plus) -- 1 (Monday) to 7 (Sunday)
W -- the day of the week -- 0 (Sunday) to 6 (Saturday)
W -- the week of the year in ISO-8601 format, starting from Monday each week (new in PHP 4.1.0) -- 42 (42nd weeks of the year)
F -- month -- January or March January to December
M -- the month represented by a number -- 01 to 12
M-the month abbreviated to three letters-Jan to Dec
N -- the month represented by a number -- 1 to 12
T -- day of the month -- 28 to 31
L -- leap year, expressed in a Boolean value -- if the leap year is 1, otherwise it is 0
O -- the year number in ISO-8601 format. Similar to Y, unless the number of weeks (W) of ISO belongs to the previous year or the next year, that year is used. (New PHP 5.1.0) -- 1999 or 2003
Y -- the year of which the four digits represent in full -- 1999 or 2003
Y -- two-digit year -- 99 or 03
A -- morning and afternoon -- am or pm
A -- morning and afternoon -- AM or PM
B -- Swatch Internet time -- 000 to 999
G-hour, 12-hour format-no leading zero 1 to 12
G -- hour, 24-hour format -- no leading zero 0 to 23
H -- hour, 12-hour format -- with a leading zero 01 to 12
H-hour, 24-hour format-with a leading zero 00 to 23
I -- minute -- 00 to 59
S -- seconds -- 00 to 59
E -- time zone ID (new in PHP 5.1.0) -- UTC, GMT, Atlanta/Azores
When I -- is enabled or not -- timeout is 1; otherwise, it is 0.
T -- Abbreviation of time zone -- for example: EST, MDT
Z -- difference from the current time zone, in seconds ---43200 to 43200
C -- date in ISO 8601 format (new PHP 5) --: 19: 21 + 00: 00
R -- date in RFC 822 format -- Thu, 21 Dec 2000 16:01:07 + 0200
U -- the overall description from 1970 1.1 to a specific time point, that is, the Unix Timestamp. For more information, see time ()
PHP date functions date formatting characters in English documentation see http://www.php.net/manual/en/function.date.php
Tips for using the date function: when you use the PHP date function to output the formatted date format, you can first write the date format you need to output, you can combine the formatted characters in the date function with ease.
Use the PHP date function date to convert the format of Unix timestamp
The second optional parameter $ timestamp of the PHP date function exists in the form of a Unix timestamp. this parameter can be used to convert the Unix timestamp to the corresponding date format. Using Unix timestamps has many advantages in parameter passing and date calculation, which is convenient and concise.
Common functions for converting a date into a Unix timestamp in PHP include mktime, time, and strtotime.
The PHP date function mktime is mainly used to convert the date and time to the Unix timestamp format.
The prototype is as follows:
Int mktime ([int $ hour = date ("H ")
[, Int $ minute = date ("I ")
[, Int $ second = date ("s ")
[, Int $ month = date ("n ")
[, Int $ day = date ("j ")
[, Int $ year = date ("Y ")
[, Int $ is_dst =-1
Note: All the parameters of the mktime function are optional. $ is_dst indicates whether the mktime function is set to expire. when the mktime function parameter is set to null, the current time is used by default, similar to the time function.
The PHP date function time is mainly used to obtain the current time, which exists in Unix timestamp format.
The PHP date function strtotime is mainly used to convert the date and time described in English to the Unix timestamp.
The prototype is as follows:
Int strtotime (string $ time [, int $ now])
Note: The $ time parameter mainly exists in the form of an English text description.
$ Now is an optional parameter that represents a benchmark for calculating the date and time of $ time. the default value is the current Unix timestamp.
The PHP date function is mainly used to convert and output various date formats. if you want to obtain all the parts of the date and time, you can pass the Unix timestamp parameter to the getdate function to obtain the data, the getdate function returns an array with the following key values:
Seconds -- seconds
Mday -- date in the month
Wday -- the day of the week, 0 (indicating Sunday) to 6 (indicating Saturday)
Mon -- month
Year -- the complete year represented by four digits
Yday-the day of the year
Weekday: indicates the text of a week, from Sunday to Saturday.
Month -- month text representation, January to December
0 -- the number of seconds since the Unix epoch, similar to the return value of time.
The above several PHP Unix timestamp functions can be used together with the PHP date function to convert, compute, and output various date formats.
PHP date calculation instance:
$ Day = 1;
$ Month = 1;
$ Year = 1989;
$ NowTime = strtotime ("last Monday ");
Echo date ("Y-m-j", $ nowTime )."
So far, PHP date function date formatting characters and some descriptions of PHP Unix timestamp functions, as well as conversion and formatting of each other's output instances have been completed, this is essential for understanding the calculation and conversion of PHP date and time functions. I hope it will be helpful for PHP beginners.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.