PHP--------Get the current time, timestamp

Source: Internet
Author: User
Tags echo date time and date

The first thing to know is that getting the time method in PHP is date (), and the timestamp method in PHP has time (), Strtotime (). described separately below.

The date () format is: Date ($format, $timestamp), format is formatted, timestamp is timestamp (optional).

Time () returns the current time of the Unix timestamp, with no parameters.

Strtotime ($time, $now) resolves the datetime description of any English text to a Unix timestamp. $time is required, specifies the time string to parse, $now the timestamp used to calculate the return value, and if omitted, the current time is used.

Example of date ($format) Usage:

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 (as a result, 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 the formatting changes, the following are the meanings of each letter in the string 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-Days of the week, three English letters; such as: "Fri"

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

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

H-hour of 24-hour system; 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 does not fill 0; such as: "0" to "23"

I-minute; such as: "00" to "59"

J-A few days, two digits, if less than two bits do 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-second; such as: "00" to "59"

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

T-the number of days in 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 day ordinal of a year; such as: "0" to "365"

Examples of time () usage:

Time (); output result: 1332427715 (the result returned is the current timestamp)

Strtotime ($time) usage examples:

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 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 (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 (Time of the previous Thursday)

The above example is so much, more of their own to work out, Strtotime () method can be used in English text control of the UNIX timestamp display, and get the required time and date format.

PHP gets the number of milliseconds in the current time

PHP itself does not provide a function to return the number of milliseconds, but provides the Microtime () method, which returns an array of two elements: one is the number of seconds, one is the number of milliseconds represented by a decimal, and we can get the number of milliseconds returned 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);}

Workaround to get the current time difference from the actual time by 8 hours:

Actual development often encounter the acquisition time and the current system actual time difference of 8 hours, this is due to the time zone setting problem, for this problem, there are several solutions:

1. Find Date.timezone in php.ini, change its value to Asia/shanghai, that is Date.timezone = Asia/shanghai (Set the current time zone to the Asian Shanghai Time Zone)

2. Add Date_default_timezone_set (' Asia/shanghai ') where the program starts;

Both ways can be, to see a person's preferences.

PHP--------Get the current time, timestamp

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.