Get current system time, timestamp in PHP

Source: Internet
Author: User
Tags echo date

Today write down Otime ($time, $now) to convert the time format to a 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-2900: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.

This blog article basically belongs to the original or collection and finishing, are painstaking crystallization.
Welcome reprint share, reproduced please indicate the source, thank you!
Original address: PHP get the current time, timestamp of various formats summary [datetime]

Get current system time, timestamp in PHP

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.