PHP Timestamp and date conversion

Source: Internet
Author: User
Tags echo date

Date converted to timestamp

PHP provides functions that can easily convert various forms of a date to a timestamp, which is mainly:

    • Strtotime (): Resolves the datetime description of any English text to a timestamp.
    • Mktime (): Gets the timestamp from the date.

Strtotime ()

The Strtotime () function is used to convert the date represented by the English text string to a timestamp, an inverse of date (), which returns the timestamp successfully, otherwise FALSE.

Grammar:

int Strtotime (string time [, int now])

The parameter time is the parsed string and is the date represented by the GNU date input format.

Example:

<?phpecho strtotime ("2009-10-21 16:00:10");//Output 1256112010echo Strtotime ("September 2008");//Output 1220976000echo Strtotime ("+1 Day"), "<br/>";//Output the timestamp of tomorrow at this time?>
Mktime ()

The Mktime () function is used to obtain a timestamp from a date, successfully returning a timestamp, or FALSE.

Grammar:

int Mktime (time, minute, second, month, day, year)

Example:

<?phpecho Mktime (21, 50, 55, 07, 14, 2010);//Output "1279115455"?>

Parameters can be omitted from right to left, and any omitted parameters are set to the current value of the cost date and time.

Mktime () is useful for date calculation and validation, and it automatically calculates the correct value for an out-of-range input. For example, the following example output is 2008-01-01:

<?phpecho date ("y-m-d", mktime (0, 0, 0, 0, 0)), Echo date ("Y-m-d", Mktime (, 0, 13, 1, 2007,)); >

The last day of next month:

The last day of any given month can be represented as the "0" day of the next month, not 1 days, as in the following example:

<?php$lastday = mktime (0, 0, 0, 3, 0,); Echo strftime ("Last Day of 2008:%d", $lastday); >

Browser output:

The last day of 2008 is: 29


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

Specified time: echo date ("Y-m-d h:i:s", Strtotime ("+1 Day") Result: 2009-01-2309: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

Specified time: echo date ("Y-m-d h:i:s", Strtotime ("1 day") Result: 2009-01-2109: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") Result: 2009-01-2909: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") Result: 2009-01-1509: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

Specified time: echo date ("Y-m-d h:i:s", Strtotime ("next Thursday") Results: 2009-01-29 00:00:00 (6) Print a timestamp that specifies the last day of the week Strtotime Thursday ")

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
The PHP timestamp Function example above shows that Strtotime can parse the datetime description of any English text into a Unix timestamp, and we get the specified timestamp by combining mktime () or date () format date time to achieve the required date time.

Example:

<?php





Time Stamp to date
$date _time_array = getdate (1297845628);//1311177600 1316865566
$hours = $date _time_array["Hours"];
$minutes = $date _time_array["Minutes"];
$seconds = $date _time_array["seconds"];
$month = $date _time_array["Mon"];
$day = $date _time_array["Mday"];
$year = $date _time_array["year"];

echo "Year: $year \nmonth: $month \nday: $day \nhour: $hours \nminutes: $minutes \nseconds: $seconds \ n";

Normal date turn timestamp
Echo mktime (0, 0, 0, 9, 18, 2011). "\ n";
Echo mktime (0, 0, 0, 9, 25, 2011). "\ n";
Turn the normal date into a timestamp, and if it is sometimes the same in seconds,

$year = ((int) substr ("2008-12-04", 0,4));//year acquired

$month = ((int) substr ("2008-12-04", 5,2));//Get month

$day = ((int) substr ("2008-12-04", 8,2));//Date

Echo mktime (0,0,0, $month, $day, $year);


?>



The timestamp is converted to the date echo "time () displays the day of the month and seconds:". Date ("Y-m-d h:i:s", Time ()). "\ n";
So even when the seconds show together
echo "Time () displays only month Day:". Date ("Y-m-d", Time ()). " \ n "//Only year Month day

echo "timestamp format:". Date ("Y-m-d h:i:s", 1297845628). "\ n";//Use timestamp directly

Y: Year (four-digit) uppercase
M: Month (two digits, first less than 0) lowercase
D: Day (two digits, first less than 0) lowercase
H: Hour with first zero 24 hour format
H: Hour with First zero 12 hour format
I: minutes with first zero
S: Seconds with First zero (00-59)
A: Lowercase noon and afternoon (AM or PM)

PHP Timestamp and date conversion

Related Article

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.