(Basic) PHP get time, time stamp of the various formats to summarize

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. Clear this, want to know more, please continue to look down.

3. Date ($format) usage

Like what:

echo Date (' y-m-d '), Output: 2016-05-22

echo Date (' y-m-d h:i:s '), Output: 2016-05-22 23:00:00

echo Date (' y-m-d ', Time ()), Output: 2016-05-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: May 22, 2016

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

Like what:

echo strtotime (' 2016-05-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: 2016-05-23 23:30:33 (will find out at the time of Tomorrow)

echo Date (' y-m-d h:i:s ', Strtotime ('-1 day ')), Output: 2016-05-21 23:30:33 (at this time yesterday)

echo Date (' y-m-d h:i:s ', Strtotime (' +1 Week ')), Output: 2016-05-29 23:30:33 (at this time next week)

echo Date (' y-m-d h:i:s ', Strtotime (' next Thursday ')), Output: 2016-05-29 00:00:00 (Time of Next Thursday)

echo Date (' y-m-d h:i:s ', Strtotime (' last Thursday ')), Output: 2016-05-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.

(Basic) PHP get time, time stamp of the various formats to summarize

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.