PHP date format turn time stamp

Source: Internet
Author: User
Tags echo date explode

PHP provides functions to easily convert various forms of dates to timestamps, which are mainly: Strtotime (): Resolves the date-time description of any English text to a timestamp. Mktime (): Gets the timestamp from the date. strtotime ()

The Strtotime () function converts the date represented by an English text string to a timestamp, the inverse of date (), returns a timestamp successfully, or FALSE. Grammar:

int Strtotime (string time [, int now])

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

Example:

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

mktime ()

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

int Mktime (time, minutes, seconds, months, days, years)

Example:

<?php
Echo mktime (N, a, m);		Output "1279115455"
?>

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

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

<?php
echo Date ("Y-m-d", mktime (0, 0, 0,,, 2007));
echo Date ("Y-m-d", mktime (0, 0, 0, 1, 2007));
? >

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

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

The following function is similar to the Strtotime function.

<?php $date _str = "2011-09-11 17:00:00"; echo $time _str = Str_format_time ($date _str); function Str_format_time ($timestamp = ') {if Preg_match (/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} (0[0-9]|1[0-9]|2[0-3)):([
		0-5][0-9]):([0-5][0-9])/i ", $timestamp)) {list ($date, $time) =explode (" ", $timestamp);
    	List ($year, $month, $day) =explode ("-", $date);
 		List ($hour, $minute, $seconds) =explode (":", $time);
	$timestamp =gmmktime ($hour, $minute, $seconds, $month, $day, $year);
	else {$timestamp =time ();
return $timestamp;
echo ' <br/> ';

echo Date ("Y-m-d h:i:s", $time _str); ? 

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.