PHP Function Reference

PHP mktime() Function



The PHP mktime() function returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Arguments may be left out in order from right to left. Any arguments thus omitted will be set to the current value according to the local date and time.

Note: Calling mktime() without arguments is deprecated. hour is no longer optional as of PHP 8.0.0. time() can be used to get the current timestamp.

Syntax

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

Parameters

hour Optional. Specify the number of the hour relative to the start of the day. Negative values reference the hour before midnight of the day. Values greater than 23 reference the appropriate hour in the following day(s).
Note: This parameter is no longer optional as of PHP 8.0.0.
minute Optional. Specify the number of the minute relative to the start of the hour. Negative values reference the minute in the previous hour. Values greater than 59 reference the appropriate minute in the following hour(s).
second Optional. Specify the number of seconds relative to the start of the minute. Negative values reference the second in the previous minute. Values greater than 59 reference the appropriate second in the following minute(s).
month Optional. Specify the number of the month relative to the end of the previous year. Values less than 1 reference the months in the previous year in reverse order, for example: 0 is December, -1 is November, etc. Values greater than 12 reference the appropriate month in the following year(s).
day Optional. Specify the number of the day relative to the end of the previous month. Values less than 1 reference the days in the previous month, for example: 0 is the last day of the previous month, -1 is the day before that, etc. Values greater than the number of days in the relevant month reference the appropriate day in the following month(s).
year Optional. Specify the number of the year, may be a two or four digit value. If it is specified in a two digit format, the values between 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999.

Return Value

Returns the Unix timestamp of the arguments given. If the arguments are invalid, the function returns false.

Exceptions

If the time zone is not valid, generates a E_WARNING.

Example: mktime() example

The example below shows the usage of mktime() function.

<?php
//setting the default timezone to use
date_default_timezone_set('UTC');

//getting the day using mktime() function
echo "May 23, 2015 was on a ". 
     date("l", mktime(0, 0, 0, 5, 23, 2015));

//providing only hour argument, rest of the
//arguments will be set to the current value 
//according to the local date and time
echo "\nToday is ". date("l", mktime(0));    
?>

The output of the above code will be:

May 23, 2015 was on a Saturday
Today is Thursday

Example: another mktime() example

This function is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input. Consider the example below:

<?php
//using date with day out-of-range -
//exceeding days (4 days) will be added
echo date("M-d-Y", mktime(0, 0, 0, 12, 35, 1995))."\n";

//using date with month out-of-range -
//exceeding months (3 months) will be added
echo date("M-d-Y", mktime(0, 0, 0, 15, 1, 2015))."\n";
?>

The output of the above code will be:

Jan-04-1996
Mar-01-2016

Example: Last day of a month

The last day of any given month can be expressed as the "0" day of the next month. Consider the example below:

<?php
$lastday = mktime(0, 0, 0, 3, 0, 2012);
echo strftime("Last day in Feb 2012 is: %d\n", $lastday);
$lastday = mktime(0, 0, 0, 4, -31, 2000);
echo strftime("Last day in Feb 2012 is: %d\n", $lastday);
?>

The output of the above code will be:

Last day in Feb 2012 is: 29
Last day in Feb 2012 is: 29

❮ PHP Date and Time Reference