Date function and timestamp function in php and format conversion tutorial between them (with examples)
In In PHP website development and construction, dates and times often need to be processed. PHP provides a variety of date and time functions to facilitate PHP developers to calculate and format dates and times.
It is very necessary to master PHP date functions, and it also lays the foundation for processing the format conversion between PHP date functions and Mysql database dates.
As the opening tutorial on PHP date functions, I use a PHP example tutorial to introduce the PHP basic date function date and Unix timestamp function as well as the conversion methods of date formats between each other.
1. PHP date and time zone setting
Before enabling the PHP date function, you first need to ensure that the set time zone is correct, otherwise the displayed date may be There are discrepancies. When setting up the PHP environment, you only need to set date.timezone to the corresponding time zone in the PHP.INI configuration file.
If you do not have the right to operate the PHP.INI configuration file, you can obtain the time zone of the current PHP running environment through the PHP time zone function date_default_timezone_get(void).
Use the date_default_timezone_set(string $timezone_identifier) function to set the corresponding time zone.
For more time zones supported by PHP, please check http://www.php.net/manual/en/timezones.php.
2. PHP format date function Date
Prototype string date(string $format[,int $timestamp])
$format - PHP The format code of the date function date
$timestamp - Unix timestamp, the default is the current timestamp (optional)
3. Format characters ————- Description ———— The return value is described as follows:
Day
d——The day of the month, returns 2 digits —— 01 to 31
j —— The day of the month, 1 to 9 without zero —— 1 to 31
S —— The English suffix of the number of days in the month —— 2 characters st, nd, rd or th. Can be used with j
z —— The day of the year —— 0 to 366
Week
l——Day of the week——Sunday to Saturday
D——English day of the week, text representation——3 letters Mon to Sun
N——Number representing the day of the week (New in PHP 5.1.0) - 1 (Monday) to 7 (Sunday)
w - Day of the week - 0 (Sunday) to 6 (Saturday)
W - The week number in the year in ISO-8601 format, each week starts on Monday (newly added in PHP 4.1.0) - 42 (the 42nd week of the year)
month(Month)
F —— month —— January or March January to December
m —— month represented by numbers —— 01 to 12
M —— The month represented by the three-letter abbreviation —— Jan to Dec
n —— The month represented by the number —— 1 to 12
t — – Number of days in the month —— 28 to 31 Is 1, otherwise it is 0
o —— ISO-8601 format year number. Similar to Y , unless the ISO week number (W) belongs to the previous or next year, that year is used. (New in PHP 5.1.0) —— 1999 or 2003 Y —— The year represented by four digits —— 1999 or 2003 y —— The year represented by two digits —— — 99 or 03
Time (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 leading zero 01 to 12
H —— Hours, 24-hour format —— With leading zeros 00 to 23
i —— Minutes —— 00 to 59
s —— Seconds —— 00 to 59
Timezone
e - Time zone identifier (newly added in PHP 5.1.0) - UTC, GMT, Atlantic/Azores
I —— Whether it is daylight saving time —— Summer time is 1, otherwise it is 0
T —— Time zone abbreviation —— For example: EST, MDT
Z —— With the current time zone Time zone difference, in seconds —— -43200 to 43200
Full Date/Time (Full Date/Time)
c — — Date in ISO 8601 format (new in PHP 5) — 2004-02-12T15:19:21 00:00
r — Date in RFC 822 format — Thu, 21 Dec 2000 16:01 :07 0200
U - General description from 1970 1.1 to a certain moment, that is, Unix timestamp, see time()
For the English documentation of PHP date function date formatting characters, please refer to http ://www.php.net/manual/en/function.date.php
Date function usage tips: When using the PHP date function date to output a formatted date format, you You can first write out the date format you need to output, and then use the formatting characters in the date function to combine it. It will be very easy to use.
4. Use the PHP date function date to convert the Unix timestamp format.
The second optional parameter $timestamp of the PHP date date function is based on the Unix timestamp. The form exists, and this parameter can be used to convert the Unix timestamp into the corresponding date format. Using Unix timestamp has many benefits in parameter passing, date calculation, etc. It is convenient and concise.
Commonly used functions in PHP to convert dates into Unix timestamps mainly include mktime, time, strtotime. PHP date function mktime is mainly used to convert date and time into 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 ]]]]]]])
mktimeAll parameters of the function are optional, where $is_dst indicates whether it is daylight saving time, when the mktime function parameters are all When empty, it will default to the current time, similar to the time function.
PHP date functiontime is mainly used to obtain the current time, which exists in Unix timestamp format. PHP date function strtotime is mainly used to convert the date and time described in English into a Unix timestamp.
The prototype is as follows
int strtotime(string $time[,int $now])
$time parameter mainly exists in the form of English text description.
The $now optional parameter mainly represents a basis for $time to calculate date and time. The default is the current Unix timestamp.
PHP date date function is mainly used to convert and output various date formats. If you want to obtain various parts of the date and time, you can obtain it by passing the Unix timestamp parameter to the getdate function, getdate The function will return a related array with the following array key values:
Seconds —- Number of seconds
Minutes —- Minutes
Hours —- Hour
mday —- Day of the month
wday —- Day of the week, 0 (for Sunday) to 6 (for Saturday)
mon —- Month
Year —- The complete year represented by four digits
yday —- The day of the year Day
Weekday —- The textual representation of the week, Sunday to Saturday
Month — The textual representation of the month, January to December
0 —- The number of seconds since the Unix epoch, and the return value of time() similar.
Various date format conversion, calculation and output can be performed through the above PHP Unix timestamp functions and the PHP date function.
5. PHP date calculation example
Calculate age
<?php $day = 1; $month = 1; $year = 1989; $birthday = mktime(0,0,0,$month,$day,$year); $nowunix = time(); $age = $nowunix - $birthday; $age = floor($age / (365*24*60*60)); echo $age; ?>
Date addition Subtraction
<?php $nowUnix = time(); $addTime = $nowUnix + (24*60*60); //日期加一天 $redTime = $nowUnix - (24*60*60);//日期减一天 echo date("Y-m-j",$addTime); echo date("Y-m-j",$redTime); ?>
6. Example of strtotime date calculation and format conversion
Date addition and subtraction can also be completed through the strtotime function
<?php $nowTime = strtotime("now"); echo date("Y-m-j",$nowTime)."<br/>"; $nowTime = strtotime("10 September 2010"); echo date("Y-m-j",$nowTime)."<br/>"; $nowTime = strtotime("+1 day"); echo date("Y-m-j",$nowTime)."<br/>"; $nowTime = strtotime("+1 week"); echo date("Y-m-j",$nowTime)."<br/>"; $nowTime = strtotime("+1 week 2 days 4 hours 2 seconds"); echo date("Y-m-j",$nowTime)."<br/>"; $nowTime = strtotime("next Thursday"); echo date("Y-m-j",$nowTime)."<br/>"; $nowTime = strtotime("last Monday"); echo date("Y-m-j",$nowTime)."<br/>"; ?>
This completes the introduction to the meaning of date formatting characters in the PHP date function, the introduction of some PHP Unix timestamp functions, and examples of conversion and formatted output between them. This is useful for mastering the calculation and conversion of PHP date and time functions. is very necessary. I hope it will be helpful to those who are starting to learn PHP.
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of Tutorial on Date function and timestamp function in PHP and format conversion between them (with examples). For more information, please follow other related articles on the PHP Chinese website!