Today write down PHP, how to get the current system time, time stamp, and note the meaning of various formats, can be flexible.
1. Get the current Time method date ()
Quite simply, this is the way to get the time, in the format: Date ($format, $timestamp), format, timestamp timestamp – The parameter can be filled.
2. Get timestamp method Time (), Strtotime ()
Both of these methods can obtain the UNIX timestamp in PHP, Time () is directly obtained, Strtotime ($time, $now) to the time format to timestamp, $time is required. Clear this, want to know more, please continue to look down.
3. Date ($format) usage
Like what:
echo Date (' y-m-d '), Output: 2012-03-22
echo Date (' y-m-d h:i:s '), Output: 2012-03-22 23:00:00
echo Date (' y-m-d ', Time ()), Output: 2012-03-22 23:00:00 (results above, just one more timestamp parameter) (method of timestamp conversion to date format)
echo Date (' Y '). ' Year '. Date (' m '). ' Month '. Date (' d '). ' Day ', output: March 22, 2012
For example, these are just some of the formatting modifications, and the following are the meanings of each letter in the format:
The meaning of letters can be used in the/************** format **************/
A – "AM" or "PM" A – "AM" or "PM" D – a few days, two digits, if less than two digits before 0; such as: "01" to "to" D – days, three English letters; such as: "Fri" F – month, English full name; such as: "January" h–12 hours of the hour system; such as: "01" to "h–24" hours of the hour system; such as: "00" to "g–12" hours of the hour system, less than two does not fill 0; such as: "1" to "g–24 hours of the hour, less than two does not fill 0;" such as: "0" to "I –" minutes; such as: "00" to "J –" A few days, two digits, if less than two does not fill 0; such as: "1" to "L –" Days of the week, English full name; such as: "Friday" M – month, two digits, if less than two digits in front of 0; such as: "01" to "three" N – month, two digits, if less than two will not fill 0; such as: "1" to "a" m – month, three English letters; such as: "Jan" s-seconds; such as: "00" to "s-" with the English ordinal number, two English letters; such as: "th", "nd" T-the number of days of the specified month; such as: "28" to "U" the total number of seconds W – number of days, such as: "0" (Sunday) to "6" (Saturday) y – years, four digits; such as: "1999" Y – year, two digits; such as: "Z –" the first day of the year; such as: "0" to "365"
4. Strtotime ($time) usage
Like what:
echo strtotime (' 2012-03-22′), output: 1332427715 (Here The result is casual, only for illustrative use)
Echo strtotime (Date (' y-d-m ')), output: (combined with date (), result ibid.) (time date converted to timestamp)
Strtotime () also has a very powerful usage, parameters can be added for the operation of numbers, month and day bloodletting characters, examples are as follows:
echo Date (' y-m-d h:i:s ', Strtotime (' +1 Day ')), Output: 2012-03-23 23:30:33 (will find out at the time of Tomorrow)
echo Date (' y-m-d h:i:s ', Strtotime ('-1 day ')), Output: 2012-03-21 23:30:33 (at this time yesterday)
echo Date (' y-m-d h:i:s ', Strtotime (' +1 Week ')), Output: 2012-03-29 23:30:33 (at this time next week)
echo Date (' y-m-d h:i:s ', Strtotime (' next Thursday ')), Output: 2012-03-29 00:00:00 (Time of Next Thursday)
echo Date (' y-m-d h:i:s ', Strtotime (' last Thursday ')), Output: 2012-03-15 00:00:00 (at the time of the previous Thursday)
Let's go to work on your own, strtotime. The () method can be used to control the display of Unix timestamps in English text, and to get the required time-date format.
5, PHP Gets the current time of the number of milliseconds
PHP itself does not provide a function to return the number of milliseconds, but provides the Microtime () method, which returns an array containing two elements: one is the number of seconds, the other is a fractional number of milliseconds, and we can get the number of milliseconds to return by this method, as follows:
function Getmillisecond () {
List ($s 1, $s 2) = explode (", microtime ());
return (float) sprintf ('%.0f ', (Floatval ($s 1) + floatval ($s 2)) * 1000);
}
6, get the current time difference 6 hours resolution method
Some friends, get the time and the current system time difference 6 hours, this is because the time zone setting problem, just set it to Shanghai time. Here's how:
1. Find Date.timezone in php.ini and change its value to Asia/shanghai, i.e. Date.timezone = Asia/shanghai
2. Add Date_default_timezone_set (' Asia/shanghai ') at the beginning of the program.
Detailed setting interpretation see: PHP Takes time error by date () function
Wordy quite a lot of, summed up all these about PHP to get the current time, timestamp, milliseconds, time difference between the various ways of writing and resolution, have any questions, welcome message to put forward, discuss together.
PHP gets a summary of the current time and time stamps in various formats