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

Source: Internet
Author: User
Tags echo date

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.

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

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 is the front 0; such as: "01" to "31"

D – Day of the week, three letters of English; such as: "Fri"

F – month, full name in English; such as: "January"

H–12 hours of the hour; such as: "01" to "12"

H–24 hours of the hour; such as: "00" to "23"

G–12 hours of the hour, less than two digits do not fill 0; such as: "1" to 12 "

G–24 hours of the hour, less than two digits do not fill 0; such as: "0" to "23"

I – minutes; such as: "00" to "59"

J – a few days, two digits, if less than two does not fill 0; such as: "1" to "31"

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 "12"

N – month, two digits, if less than two digits does not fill 0; such as: "1" to "12"

M – month, three letters of English; such as: "Jan"

S-seconds; such as: "00" to "59"

S-with English ordinal number, two English letters; such as: "th", "nd"

T-the number of days of the specified month; such as: "28" to "31"

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: "99"

Z – The first day of the year; such as: "0" to "365"

4, Strtotime ($time) Usage
Example:
Echo strtotime (' 2012-03-22′), output: 1332427715 (The result here is casually written, 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 to the operation of the number, Year 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 this time 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 (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 (Time of last Thursday)
et cetera, go to work on your own, Strtotime () 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

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

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.