You need to know a few concepts about time: timezone/World time/unix timestamp
1. Time zone
This concept has been heard a lot before. We have to say a few words, we use real life in the real area, in the computer is also the same rules.
When the international Longitude Conference was held in Washington in 1884, in order to overcome the chaos of time, the world was divided into 24 time zones.
In China, the capital Beijing is located in the East eight district time for the national uniform use time.
2. The GMT
Not only are astronomers using Greenwich Mean Time (abbreviated as GMT), it is also often seen in newspapers and periodicals. We know that there are local times everywhere. If there is a major international event, the local time to record, you will feel complex inconvenience. And it's easy to make mistakes in the days ahead. As a result, astronomers have come up with an acceptable and convenient way to keep track of the local time in Greenwich, a region of the UK.
3.unix time Stamp
The computer itself does not know the time, we set a time in the computer convenient operation. So we have a way of calculating Unix timestamps.
The number of seconds elapsed from the Unix era (January 1, 1970 0 o'clock) to a time.
1. Set the time zone
If we are a multinational multi-language party program, we usually write a time zone in the configuration file, each time the program runs. Will read the settings in this time zone to show the time.
The functions for setting the time zone are:
1). Date_default_timezone_get ()
2). Date_default_timezone_set ()
The first function we do not focus on the explanation, relatively simple.
Use the following:
String Date_default_timezone_get (void)
Function:
Gets the default time zone used by all DateTime functions in a script
<?php
echo date_default_timezone_get ();
?>
The second function is the focus:
Use the following:
BOOL Date_default_timezone_set (string $timezone _identifier)
Function: The default time zone for all datetime functions
Example:
<?php
//定义一下时区常量,以后你可以放到配置文件里
define(‘TIME_ZONE‘,‘Asia/shanghai‘);
//执行函数
date_default_timezone_set(TIME_ZONE);
echo date(‘Y-m-d H:i:s‘);
?>
Note: Please see the official manual for the time zone list http://php.net/manual/zh/timezones.php
2.time () Gets the current UNIX timestamp
The function of the time () function is to get the Unix timestamp of the current time.
The following code outputs the UNIX timestamp for the current time.
<?php
$time=time();
print_r( $time);
?>
3. "YMD" is the key to PHP learning time processing
- Y is year in English and represents years for year
- M represents month in English, for the months
- D English Representative day, for the date represented
So we need to output the previous year, month, and date words.
For example: July 1, 1997, we can use the above three parameters.
<?php
echo date(‘Y年m月d日‘);
?>
You can run the code and try it out and see if it shows up.
There are a few more parameters later:
H:m:s
It stands for: time and minute
- h in English: Hour on behalf of the hour
- I in English: minute stands for minutes
- S in English: second stands for seconds
<?php
//就可以显示出来当前的时间了哟。
echo date(‘Y-m-d H:i:s‘);
?>
date函数
Used to format the output of a time for easy display or storage of time. The syntax format is as follows:
String Date (string F oRRNaT[,INT f o r r n a t [ , i n t Tirnestamp])
In the parameter list:
character |
Description |
return value |
D |
The day ordinal of a month with a leading zero 2-digit number |
01 to 31 |
D |
English day of the week, 3 letters |
Mon to Sun |
J |
The day ordinal of the month without leading zeros |
1 to 31 |
L (Letter) |
English Day of the week |
Sunday to Saturday |
N |
1 format number of the week represented |
1 (for Monday) to 7 (= Sunday) |
S |
English suffix, 2 characters, after the number of days per month |
st,nd,rd or th. Can be used with the JG. |
W |
Day of the week, numbers indicate |
0 (for Sunday) to 6 (= Saturday) |
Z |
The day ordinal of a year |
0 to 366 |
W |
Week of the year, starting from Monday |
42 (42nd Week of the year) |
F |
month, full text format |
January to December |
M |
The number represents the month with a leading zero |
01 to 12 |
M |
3 letter abbreviation for month |
Jan to Dec |
N |
Number indicates month, no leading zero |
1 to 12 |
T |
The number of days that a given month should be |
28 to 31 |
L |
Whether it is a leap year |
If the leap year is 1, otherwise o |
O |
Format Year number |
For example 2007 |
Y |
4-digit full year indication |
For example, 1999 or 2008 |
Y |
Year 2-digit representation |
For example, 99 or 08 |
A |
Lowercase morning and afternoon values |
AM or PM |
A |
Uppercase morning and afternoon values |
AM or PM |
G |
Hours, 12-hour format, no leading zeros |
1 to 12 |
G |
Hours, 24-hour format, no leading zeros |
0 to 23 |
I |
Number of minutes with leading zeros |
00 to 59 |
S |
Number of seconds with leading zeros |
00 to 59 |
E |
Time zone identification |
|
U |
Number of seconds since the beginning of the UNIX era |
Long-integer Numbers |
3.getdate Get current system time
getdate
Used to get the current system time, or to get a timestamp of the specific meaning. The timestamp is a long integer that represents the syntax format of the getdate as shown below.
Array getdate ([int $timestamp = time ()])
The return value of a function is an array of time information obtained from timestamp. If there are no parameters, the current time is returned. GETDATE returns an array of key names that include full information about the time and date.
Key Name |
description |
return value |
secnods |
seconds |
digits 0 to 0 |
minutes |
minutes |
number from + to + |
hours |
hours |
number 0 to + |
mday |
month of day |
number 1 to + |
wday |
Day of the week |
number 0 (for Sunday) to 6 (for Saturday) |
Mon |
Month |
number 1 through |
Year |
years |
4-digit full year |
yday |
Day of the year |
number 0 to 365 |
Weekday |
weeks of English |
Sunday to Saturday |
month |
English |
January to December |
0 |
number of seconds since the Unix era |
long integer number |
The following code can return the details of the getdate array
<?php
$mytime= getdate();
print_r( $mytime);
?>
Print_r can output all the key names and values in an array.
The program outputs the time and date details of the current computer:
Array
(
[seconds] => 1 //秒
[minutes] => 10 //分钟
[hours] => 17 //小时
[mday] => 18 //日
[wday] => 0 //星期中的第几天
[mon] => 1 //月
[year] => 2015 //年
[yday] => 17 //年中的第几天
[weekday] => Sunday //星期
[month] => January //月份
[0] => 1421597401 //时间戳
)
After understanding the GETDATE function and the returned array, it is easy to get the current time information.
The GETDATE function obtains time information and calls the value of the return time array to output time information.
<?php
$mytime = getdate();
echo "年 :".$mytime[‘year‘]."\n";
echo "月 :".$mytime[‘mon‘]."\n";
echo "日 :".$mytime[‘mday‘]."\n";
echo "时 :".$mytime[‘hours‘]."\n";
echo "分 :".$mytime[‘minutes‘]."\n";
echo "秒 :".$mytime[‘seconds‘]."\n";
echo "一个小时中的第几钟 :".$mytime[‘minutes‘]."\n";
echo "这是一分钟的第几秒 :".$mytime[‘seconds‘]."\n";
echo "星期名称 :".$mytime[‘weekday‘]."\n";
echo "月份名称 :".$mytime[‘month‘]."\n";
echo "时间戳 :".$mytime[0]."\n";
?>
From for notes (Wiz)
Front-end PHP entry -020-key Date function acquisition period time information function