PHP Gets the current timestamp function detailed explanation

Source: Internet
Author: User
Tags echo date time and date timestamp to date
There are a variety of ways to get timestamps in PHP, and usually get time is the UNIX timestamp and we normally see the time is not the same, then we need to format this, this article is about PHP gets to the current time and PHP to the UNIX timestamp format, Don't say a word, let's take a look!

PHP timestamp function Gets the UNIX timestamp of the specified date strtotime (' 2012-12-7 ')
Examples are as follows:

The code is as follows:

<?php     Echo strtotime (' 2012-12-7 ');//results: 1354838400?>

Note: Return December 7, 2012 0:0 0 seconds timestamp.

Converts the current month day to timestamp: the PHP timestamp function gets the Unix timestamp for the current date,
Examples are as follows:

The code is as follows:

<?php     Echo $time =intval (Time ());?>

Ming: Returns the timestamp of the current time, month and day.
Convert the timestamp to date:

The code is as follows:

<?php     echo Date ("Y-m-d h:i:s", Intval (Time ()));?>

The code is as follows:

<?php Echo strtotime ("Now"), "n"; Echo Strtotime ("Ten September"), "n"; Echo strtotime ("+1 Day"), "n"; Echo strtotime ("+1 Week"), "n"; Echo strtotime ("+1 Week 2 days 4 hours 2 Seconds"), "n"; Echo Strtotime ("next Thursday"), "n"; Echo Strtotime ("Last Monday"), "n";?>

PHP timestamp function Get English text date time
Easy to compare, use date to convert time stamp to system time with specified timestamp

(1) Print the timestamp for tomorrow at this time Strtotime ("+1 Day")
Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25
Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("+1 Day") Results: 2009-01-23 09:40:25

(2) Print the timestamp at this time yesterday Strtotime ("-1 day")
Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25
Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("1 day") Results: 2009-01-21 09:40:25

(3) Print timestamp strtotime ("+1 Week") at this time next week
Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25
Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("+1 Week") Results: 2009-01-29 09:40:25

(4) Print timestamp strtotime ("1 week") at this time last week
Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25
Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("1 Week") Results: 2009-01-15 09:40:25

(5) Print the time stamp specified next week strtotime ("next Thursday")
Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25
Time Specified: Echo date ("Y-m-d h:i:s", Strtotime ("next Thursday") Results: 2009-01-29 00:00:00

(6) Print the timestamp strtotime ("last Thursday") specified on the previous day of the week
Current Time: Echo Date ("Y-m-d h:i:s," time ()) Results: 2009-01-22 09:40:25
Specified time: echo date ("Y-m-d h:i:s", Strtotime ("last Thursday")) Results: 2009-01-15 00:00:00

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

4. Strtotime ($time) usage
Like what:
echo strtotime (' 2012-03-22 '), output result:1332427715 (Here The result 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, 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 be found at the time of the output 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 of 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 result: 2012-03-15 00:00:00 (the time of last Thursday)
et cetera, go to work on your own, Strtotime () method can be used to control the display of Unix timestamp by English text, and get the time and 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) + Floa Tval ($s 2)) * 1000);}

Guess you might be interested in:

Common ways to use PHP datetime

In this paper, the methods used by the DateTime object are collated to make it easy for us to find and flip through, so we can refer to learning.

PHP converts a DateTime object into an implementation code for a friendly time display

PHP converts a DateTime object into an implementation code for a friendly time display. Copy the code code as follows: ...

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.