PHP Gets the current time, timestamp in various formats summary [datetime]

Source: Internet
Author: User
Tags echo date

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--can be filled in parameters.
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 "D"-Day of the week, three letters of English; such as: "Fri" F-month, English full name; such as: "January" h-12 hours of the hour system; such as: "01" to "a" 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 three "G-24 hours of the hour, less than two does not fill 0; such as: "0" to "the" I-minute; such as: "00" to "the" J-a few days, two digits, if less than two bits do not fill 0; such as: "1" to "~" L-day 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 "ten" M-month, three English letters; such as: "Jan" s-second; such as: "00" to "the" S-the end of the English ordinal, two English letters; such as: "th", "nd" T-Specify the number of days of the month; e.g. "28" to "U"-Total number of seconds W-number of days of the week, such as: "0" (Sunday) to "6" (Saturday) Y-year, 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 random, 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.

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.