PHP date function date format conversion example tutorial

Source: Internet
Author: User
Tags time zones iso 8601 iso 8601 format php example
So far, PHP date function date formatting characters and descriptions of some PHPUnix 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. PHP often needs to process the date and time during Website Development and Construction. PHP provides various date and time functions to facilitate PHP developers to calculate and convert the date and time formats. 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. As the starting tutorial of PHP date functions, I used the PHP example tutorial to introduce the date and Unix timestamp functions of PHP basic date functions and the 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 do not have the permission to operate the PHP. INI configuration file, you can use the PHP time zone function date_default_timezone_get (void) to obtain the time zone of the current PHP running environment, and then useDate_default_timezone_set (string $ timezone_identifier)The function sets the corresponding time zone. for more PHP-supported time zones, see http://www.php.net/manual/en/timezones.php.

  PHP format Date function

1
2
3
4
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 Characters----Description---Return value description

  Day)

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

  Week (Week)

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)

  Month)

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

  Year)

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

  Time)

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

  Time zone)

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

  Full Date/Time)

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

  Date function usage tips: When you use the PHP date function to output the formatted date format, you can first write the date format you need to output, and then use the formatted characters in the date function for combination, it is easy to use.

  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.

1
2
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])

All the parameters of the mktime function are optional. $ is_dst indicates whether the mktime function is the latest time. when the mktime function parameter is 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.

1
2
3
4
5
The prototype is as follows:
Int strtotime (string $ time [, int $ now])

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
Minutes-minute
Hours-Hour
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 computing instance

  Age calculation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ Day = 1;
$ Month = 1;
$ Year = 1989;

$ Birthday = mktime (0, 0, $ month, $ day, $ year );

$ Nowunix = time ();

$ Age = $ nowunix-$ birthday;

$ Age = floor ($ age/(365*24*60*60 ));

Echo $ age;
?>

  Date addition/subtraction

1
2
3
4
5
6
7
8
9
10
$ NowUnix = time ();

$ AddTime = $ nowUnix + (24*60*60); // date plus one day

$ RedTime = $ nowUnix-(24*60*60); // date minus one day

Echo date ("Y-m-j", $ addTime );
Echo date ("Y-m-j", $ redTime );
?>

  Strtotime date calculation and format conversion instance

You can also use the strtotime function to add or subtract a date.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ NowTime = strtotime ("now ");
Echo date ("Y-m-j", $ nowTime )."
";

$ NowTime = strtotime ("10 September 2010 ");
Echo date ("Y-m-j", $ nowTime )."
";

$ NowTime = strtotime ("+ 1 day ");
Echo date ("Y-m-j", $ nowTime )."
";

$ NowTime = strtotime ("+ 1 week ");
Echo date ("Y-m-j", $ nowTime )."
";

$ NowTime = strtotime ("+ 1 week 2 days 4 hours 2 seconds ");
Echo date ("Y-m-j", $ nowTime )."
";

$ NowTime = strtotime ("next Thursday ");
Echo date ("Y-m-j", $ nowTime )."
";

$ 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.

Related Article

Contact Us

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.