PHP function to obtain period time information

Before formally learning the date function, you must understand a few concepts:

1. Time zone

2. Universal time

3.Unix timestamp

Time zone

This concept has been heard a lot before. Let's say a few words. The real areas we use in real life are also regulated in computers.
When the International Longitude Conference was held in Washington in 1884, in order to overcome time confusion, the world was divided into 24 time zones.
In China, the time in Dongba District, where the capital Beijing is located, is used as the unified time for the whole country.

Universal Time

Not only astronomers use Greenwich Time (English abbreviation: GMT), but this term also often appears in news newspapers. We know that there are local times everywhere. If we use local time to record a major international event, it will be complicated and inconvenient. And it will be easy to make mistakes as time goes by. Therefore, astronomers proposed a recording method that is acceptable and convenient for everyone, which is to use the local time in Greenwich (a region in the UK) as the standard.

unix timestamp

The computer itself does not know time. We set a time in the computer to facilitate calculations. So we specified a calculation method, Unix timestamp.
The number of seconds elapsed from the Unix epoch (0:00 on January 1, 1970) to a time.

We have learned a few concepts, now we can start to learn the time function.

1. Set the time zone

If we are a multinational multilingual program, we usually write a time zone in the configuration file, each time when the program is running. The time zone settings will be read to display the time.

The function to set the time zone is:
1).date_default_timezone_get()
2).date_default_timezone_set()

We will not focus on the first function, it is relatively simple.

Usage is as follows:

string date_default_timezone_get (void)

Function is as follows:
Get the default value used by all date and time functions in a script Time zone

Example:

This displays the currently set time.

The second function is the key point:

The usage is as follows:

bool date_default_timezone_set (string $timezone_identifier)

The functions are as follows:
Default time zone used for all date and time functions

Example:

Try comparing the code of the above example, and then comment out date_default_timezone_set and see what will be prompted.

Note:
For the time zone list, please see the official manual http://php.net/manual/zh/timezones.php

2.time() to obtain the current unix timestamp

The function of time() function is to obtain the Unix timestamp of the current time.

The following code outputs the Unix timestamp of the current time.

Program running result:
1421597858

3. "Flax drop" is the key to learning time processing in PHP

Y The English word is year, which means year - —亚

m English represents month, which represents the month——hem

d English represents day, which represents the date——fall

So we need to output the year before , month, date. For example: July 1, 1997, we can use the above three parameters.

You can try running the code to see if it is displayed.

There are several parameters after:

H:m:s represents: hours, minutes and seconds

h in English is: hour represents hours

The English version of i is: minute, which represents minutes. The English version of s is: second, which represents seconds.

If written in full, it is:

The date function is used to format a time. Output to facilitate time display or storage. The syntax format is as follows:

string date ( string $forrnat [, int $tirnestamp] )

In the parameter list:

$timestamp is a timestamp, and the function presses this timestamp into $format Output in the specified format.

If $timestamp has no input value, it defaults to the current time.

$format is a string of time output format, and the specified characters need to be used to construct the output format.

Format parameter table of date function:


3. getdate gets the current system time

getdate is used to get the current system time, or get the specific meaning of a timestamp. The timestamp is a long integer, and the syntax format of getdate is as follows.

array getdate ([ int $timestamp = time() ] )

The return value of the function is an array containing time information obtained based on timestamp. If there are no parameters, the current time will be returned. The array returned by getdate, the key name includes complete information of time and date.

##D Day of the week in English, 3 letters Mon to Sun j The day of the month without leading zeros 1 to 31 l(letter) Day of the week in English Sunday to Saturday N 1 Format number represents the day of the week 1 (meaning Monday) to 7 (meaning Sunday) S The English suffix after the number of days in each month, 2 characters st, nd, rd or th. Can be used with jg w The day of the week, the number represents 0 (indicating Sunday) to 6 (indicating Saturday) z Day of the year 0 to 366 W The week in the year, each week starts on Monday 42 (the 42nd week of the year) F month , complete text format January to December m The number represents the month, with leading zeros 01 to 12 M 3 letter abbreviation for the month Jan to Dec n The number represents the month, without leading zeros 1 to 12 t The number of days in a given month 28 to 31 L Whether it is a leap year If it is a leap year, it is 1, otherwise it is o o Format year number For example, 2007 Y 4 digits represent the complete year For example, 1999 or 2008 y 2-digit year For example, 99 or 08 a Lowercase AM and PM values am or pm ##A g G i s e ##U Since the Unix epoch Number of seconds so far Long integer number
Characters Description Return value
d The day of the month, 2 digits with leading zeros 01 to 31
Uppercase AM and PM values AM or PM
hour, 12-hour format, no leading zeros 1 to 12
Hour, 24-hour format, no leading zeros 0 to 23
Yes Minutes with leading zeros 00 to 59
Seconds with leading zeros 00 to 59
Time zone identifier
## secnods Seconds Numbers 0 to 59 minutes Minutes Numbers 0 to 59 hours Hours Numbers 0 to 23 mday The first day of the month Days Numbers 1 to 31 wday Days of the week Numbers 0 (meaning Sunday) to 6 (Indicates Saturday) mon Month Number 1 to 12 year Year Complete year represented by 4 digits yday Day of the year Number 0 To 365 ##weekday month 0
Key name Description Return value
Day of the week in English Sunday to Saturday
Month in English January to December
The number of seconds since the Unix epoch Long integer Number


The following code can return the details of the getdate array.

print_r can output all the key names and values in an array. Running this code, the results are as follows. 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 obtain the current time information. The following code uses the getdate function to obtain time information, and calls the value of the returned time array to output the time information.

Run this program and the detailed information of the current time will be displayed. The results of running the program are as follows.

Year:2015

Month:1Day:18
Hour:17
Minute:14
Second:11
The first day of the week Day: 14
Day of the year: 11
Name of week: Sunday
Name of month: January
Time stamp: 1421597651

Continuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!