This example describes how to convert the date format of the PHP date function. 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, 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 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
Day)
1
D -- the day of the month, returns two digits -- 01 to 31
2
J -- the day of the month, 1 to 9 without zero -- 1 to 31
3
S-English suffix of the number of days in a month-2 characters (st, nd, rd, or th. Can be used with j
4
Z -- the day of the year -- 0 to 366
Week (Week)
1
L -- day of the week -- Sunday to Saturday
2
D: The day of the week in English. the text indicates the day of the week from Mon to Sun.
3
N -- represent the day of the week in numbers (PHP 5.1.0 plus) -- 1 (Monday) to 7 (Sunday)
4
W -- the day of the week -- 0 (Sunday) to 6 (Saturday)
5
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)
Month)
1
F -- month -- January or March January to December
2
M -- the month represented by a number -- 01 to 12
3
M-the month abbreviated to three letters-Jan to Dec
4
N -- the month represented by a number -- 1 to 12
5
T -- day of the month -- 28 to 31
Year)
1
L -- leap year, expressed in a Boolean value -- if the leap year is 1, otherwise it is 0
2
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
3
Y -- the year of which the four digits represent in full -- 1999 or 2003
4
Y -- two-digit year -- 99 or 03
Time)
1
A -- morning and afternoon -- am or pm
2
A -- morning and afternoon -- AM or PM
3
B -- Swatch Internet time -- 000 to 999
4
G-hour, 12-hour format-no leading zero 1 to 12
5
G -- hour, 24-hour format -- no leading zero 0 to 23
6
H -- hour, 12-hour format -- with a leading zero 01 to 12
7
H-hour, 24-hour format-with a leading zero 00 to 23
8
I -- minute -- 00 to 59
9
S -- seconds -- 00 to 59
Time zone)
1
E -- time zone ID (new in PHP 5.1.0) -- UTC, GMT, Atlanta/Azores
2
When I -- is enabled or not -- timeout is 1; otherwise, it is 0.
3
T -- Abbreviation of time zone -- for example: EST, MDT
4
Z -- difference from the current time zone, in seconds ---43200 to 43200
Full Date/Time)
1
C -- date in ISO 8601 format (new PHP 5) --: 19: 21 + 00: 00
2
R -- date in RFC 822 format -- Thu, 21 Dec 2000 16:01:07 + 0200
3
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:
1
Int mktime ([int $ hour = date ("H ")
2
[, Int $ minute = date ("I ")
3
[, Int $ second = date ("s ")
4
[, Int $ month = date ("n ")
5
[, Int $ day = date ("j ")
6
[, Int $ year = date ("Y ")
7
[, Int $ is_dst =-1
8
])
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:
1
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:
01
Seconds -- seconds
02
Minutes-minute
03
Hours-Hour
04
Mday -- date in the month
05
Wday -- the day of the week, 0 (indicating Sunday) to 6 (indicating Saturday)
06
Mon -- month
07
Year -- the complete year represented by four digits
08
Yday-the day of the year
09
Weekday: indicates the text of a week, from Sunday to Saturday.
10
Month -- month text representation, January to December
11
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:
Age calculation
01
02
$ Day = 1;
03
$ Month = 1;
04
$ Year = 1989;
05
06
$ Birthday = mktime (0, 0, $ month, $ day, $ year );
07
08
$ Nowunix = time ();
09
10
$ Age = $ nowunix-$ birthday;
11
12
$ Age = floor ($ age/(365*24*60*60 ));
13
14
Echo $ age;
15
?>
Date addition/subtraction
01
02
$ NowUnix = time ();
03
04
$ AddTime = $ nowUnix + (24*60*60); // date plus one day
05
06
$ RedTime = $ nowUnix-(24*60*60); // date minus one day
07
08
Echo date ("Y-m-j", $ addTime );
09
Echo date ("Y-m-j", $ redTime );
10
?>
Strtotime date calculation and format conversion instance
You can also use the strtotime function to add or subtract a date.
01
02
$ NowTime = strtotime ("now ");
03
Echo date ("Y-m-j", $ nowTime )."
";
04
05
$ NowTime = strtotime ("10 September 2010 ");
06
Echo date ("Y-m-j", $ nowTime )."
";
07
08
$ NowTime = strtotime ("+ 1 day ");
09
Echo date ("Y-m-j", $ nowTime )."
";
10
11
$ NowTime = strtotime ("+ 1 week ");
12
Echo date ("Y-m-j", $ nowTime )."
";
13
14
$ NowTime = strtotime ("+ 1 week 2 days 4 hours 2 seconds ");
15
Echo date ("Y-m-j", $ nowTime )."
";
16
17
$ NowTime = strtotime ("next Thursday ");
18
Echo date ("Y-m-j", $ nowTime )."
";
19
20
$ NowTime = strtotime ("last Monday ");
21
Echo date ("Y-m-j", $ nowTime )."
";
22
?>
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.
Bytes. So...