PHP Date time strtotime mktime


PHP provides a function can be easily converted to various forms of the date of time stamp, the function is mainly:

  • strtotime(): parses the date-time description of any English text as a timestamp.
  • mktime(): Get the timestamp from the date.


strtotime()

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

grammar:

int strtotime(string time[, int now])

The parameter time is the string to be parsed and is the date according to the GNU date input format.

example:

echo strtotime("2009-10-21 16: 00: 10 "); // output 1256112010
echo strtotime("10 September 2008 "); // output 1220976000
echo strtotime("+ 1 day ")," ≪br />"; // output the timestamp of tomorrow at this time
?>

mktime()

The mktime() function is used to retrieve the timestamp from the date and return the timestamp. Otherwise, it returns FALSE.

grammar:

int mktime(hour, minute, second, month, day, year)

example:

echo mktime(21, 50, 55, 07, 14, 2010); // output "1279115455"
?>

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

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

echo date("Y-m-d", mktime(0, 0, 0, 12, 32, 2007));
echo date("Y-m-d", mktime(0, 0, 0, 13, 1, 2007));
?>

On the last day of the following month:

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

$lastday = mktime(0, 0, 0, 3, 0, 2008);
echo strftime("2008 last day is:% d
", $lastday);
?>

Browser output:

The last day of 2008 is: 29



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook