Description

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.

Notes

Note:

As of PHP 5.1, when called with no arguments, mktime()
throws an E_STRICT notice: use the
time() function instead.

Parameters

hour

The number of the hour relative to the start of the day determined by
month, day and year.
Negative values reference the hour before midnight of the day in question.
Values greater than 23 reference the appropriate hour in the following day(s).

minute

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

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

The number of the month relative to the end of the previous year.
Values 1 to 12 reference the normal calendar months of the year in question.
Values less than 1 (including negative values) reference the months in the previous year in reverse order, so 0 is December, -1 is November, etc.
Values greater than 12 reference the appropriate month in the following year(s).

day

The number of the day relative to the end of the previous month.
Values 1 to 28, 29, 30 or 31 (depending upon the month) reference the normal days in the relevant month.
Values less than 1 (including negative values) reference the days in the previous month, so 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

The number of the year, may be a two or four digit value,
with values between 0-69 mapping to 2000-2069 and 70-100 to
1970-2000. On systems where time_t is a 32bit signed integer, as
most common today, the valid range for year
is somewhere between 1901 and 2038. However, before PHP 5.1.0 this
range was limited from 1970 to 2038 on some systems (e.g. Windows).

is_dst

This parameter can be set to 1 if the time is during daylight savings time (DST),
0 if it is not, or -1 (the default) if it is unknown whether the time is within
daylight savings time or not. If it's unknown, PHP tries to figure it out itself.
This can cause unexpected (but not incorrect) results.
Some times are invalid if DST is enabled on the system PHP is running on or
is_dst is set to 1. If DST is enabled in e.g. 2:00, all times
between 2:00 and 3:00 are invalid and mktime() returns an undefined
(usually negative) value.
Some systems (e.g. Solaris 8) enable DST at midnight so time 0:30 of the day when DST
is enabled is evaluated as 23:30 of the previous day.

Note:

As of PHP 5.1.0, this parameter became deprecated. As a result, the
new timezone handling features should be used instead.

Note:

This parameter has been removed in PHP 7.0.0.

Return Values

mktime() returns the Unix timestamp of the arguments
given.
If the arguments are invalid, the function returns FALSE (before PHP 5.1
it returned -1).

Errors/Exceptions

Every call to a date/time function will generate a E_NOTICE
if the time zone is not valid, and/or a E_STRICT
or E_WARNING message
if using the system settings or the TZ environment
variable. See also date_default_timezone_set()

Changelog

Version

Description

7.0.0

is_dst parameter has been removed.

5.3.0

mktime() now throws E_DEPRECATED notice
if the is_dst parameter is used.

5.1.0

The is_dst parameter became deprecated.
Made the function return FALSE on error, instead of
-1.
Fixed the function to accept the year, month and day to be all passed
as zero.

5.1.0

When called with no arguments, mktime() throws
E_STRICT notice. Use the
time() function instead.

5.1.0

Now issues the E_STRICT and E_NOTICE
time zone errors.

Examples

Example #1 mktime() basic example

<?php// Set the default timezone to use. Available as of PHP 5.1date_default_timezone_set('UTC');

mktime() is useful for doing date arithmetic
and validation, as it will automatically calculate the correct
value for out-of-range input. For example, each of the following
lines produces the string "Jan-01-1998".

User Contributed Notes 27 notes

Do remember that, counter-intuitively enough, the arguments for month and day are inversed (or middle-endian). A common mistake for Europeans seems to be to feed the date arguments in the expected order (big endian or little endian).

It's clear to see where this weird order comes from (even with the date being big endian the order for all arguments would still be mixed - it's obviously based on the American date format with the time "prefixed" to allow an easier shorthand) and why this wasn't changed (passing the values in the wrong order produces a valid, though unexpected, result in most cases), but it continues to be a source of confusion for me whenever I come back to PHP from other languages or libraries.

Please note, mktime requires an integer value, if you use date("H"), date("i"), date("s") as a value, which is actually have a leading zero, you may get "A non well formed numeric value encountered" notice. so you need some tricks like this

Just a small thing to think about if you are only trying to pull the month out using mktime and date. Make sure you place a 1 into day field. Otherwise you will get incorrect dates when a month is followed by a month with less days when the day of the current month is higher then the max day of the month you are trying to find.. (Such as today being Jan 30th and trying to find the month Feb.)

Pay attention that not all days have the same number of seconds (86400s) if you are using date_default_timezone_set(..) and the used timezone has Daylight Saving Time (DST) e.g. "Europe/Berlin". Under PHP 5.5.16 I get the following results:

The maximum possible date accepted by mktime() and gmmktime() is dependent on the current location time zone.

For example, the 32-bit timestamp overflow occurs at 2038-01-19T03:14:08+0000Z. But if you're in a UTC -0500 time zone (such as EST in North America), the maximum accepted time before overflow (for older PHP versions on Windows) is 2038-01-18T22:14:07-0500Z, regardless of whether you're passing it to mktime() or gmmktime().

$d_firstdow = date('w', mktime(0,0,0,$myMonth,'1',$myYear)); // FIRST falls on what day of week (0-6)$d_firstname = date('l', mktime(0,0,0,$myMonth,'1',$myYear)); // FIRST falls on what day of week Full Name

$d_weeksleft = (52 - $d_weekofyear); // how many weeks left in year$d_daysinyear = $d_isleapyear ? 366 : 365; // set correct days in year for leap years$d_daysleft = ($d_daysinyear - $d_dayofyear); // how many days left in year?>

By using date outputs inside mktime and adding or subtracting from them may be simpler than using other methods (string concatenations or timestamp values) and less prone to human calculations' errors.

The following function moves all the parameters in order of most significant (biggest) to least significant (smallest) order.Year is bigger than month. Month is bigger than day. Day bigger than hours...

Much less confusing than mktime order.

<?phpfunction mkTimestamp($year,$month,$day, $hours=0,$minutes=0,$seconds=0){// Same as mktime() but parameters are in most significant to least significant order.return mktime($hours,$minutes,$seconds, $month,$day,$year);}?>

Excel uses "number of days since Jan. 1, 1900" to store its dates. It also treats 1900 as a leap year when it wasn't, thus there is an extra day which must be accounted for in PHP (and the rest of the world). Subtracting 1 from Excel's number will fix this problem.

There are several warnings here about using mktime() to determine a date difference because of daylight savings time. However, nobody seems to have mentioned the other obvious problem, which is leap years.

Leap years mean that any effort to use mktime() and time() to determine the age (positive or negative) of some timestamp in years will be flawed. There are some years that are 366 days long, therefore you cannot say that there is a set number of seconds per year.

Timestamps are good for determining *real* time, which is not the same thing as *human calendar* time. The Gregorian calendar is only an approximation of real time, which is tweaked with daylight savings time and leap years to make it conform more to humans' expectations of how time should or ought to work. Timestamps are not tweaked and therefore are the only authoritative way of recording in computers a proper order of succession of events, but they cannot be integrated with a Gregorian system unless you take both leap years and DST into account. Otherwise, you may get the wrong number of years when you are approaching a value of exactly X years.

As for PHP, you could still use timestamps as a way of determining age if you took into account not only DST but also whether or not each year is a leap year and adjusted your calculations accordingly. However, this could become messy and inefficient.

There is an alternative approach to calculating days given the day, month and year of the dates to be compared. Compare the years first, and then compare the month and day - if the month and day have already passed (or, if you like, if they match the current month and day), then add 1 to the total for the years.

This solution works because it stays within the Gregorian system and doesn't venture into the world of timestamps.

There is also the issue of leap seconds, but this will only arise if you literally need to get the *exact* age in seconds. In that case, of course, you would also need to verify that your timestamps are exactly correct and are not delayed by script processing time, plus you would need to determine whether your system conforms to UTC, etc. I expect this will hardly be an issue for anybody using PHP, however if you are interested there is an article on this issue on Wikipedia:

Please note that incrementing a date using mktime in a loop is not proper. You could do it, except that there is a far better method found in the DateTime PHP class. Look at the documentation for DateTime::modify, DateTime::add (when supported) and DateTime::sub (when supported).

Also, adding seconds to a time is, well it isn't as easy as it seems, "Hey I'll just add 3600 seconds or 86400 seconds or x seconds!". The phrase once bitten, twice shy is quite applicable with the usage of adding seconds. If you ever had to 'fix' a time by calculating midnight to add the correct number of seconds, then you are doing it wrong.

Luckily, knowing is not a requirement, because DateTime and friends exists, removing the complexity for you.

So if given a choice of

mktime($seconds, $minutes, $hours+1);

and

$datetime->modify('+1 hour');

or

$datetime->add('P1H');

I'll go with the second choice, but probably not the third, unless I was using DateInterval::createFromDateString, so that other developers knew my intent.

Here is what I use to calculate age. It took me 30 minutes to write and it's quite accurate. What it has special is that it's calculating the number of days a year has (float number), by testing if a year is a leap one or not. This number is used to compute the age.