mktime()
Learn the PHP mktime() function: its argument order, automatic date normalization, timezone behavior, and how to do date math safely.
The PHP mktime() function builds a Unix timestamp from individual date and time parts — hour, minute, second, month, day, and year. It is the inverse of date(): where date() turns a timestamp into a formatted string, mktime() turns the parts of a date into a timestamp you can store, compare, or do arithmetic on.
This chapter covers the argument order (which trips up almost everyone), how mktime() automatically normalizes out-of-range values, its timezone behavior, and when you should reach for the DateTime classes instead.
Syntax
mktime(
int $hour = (current hour),
?int $minute = (current minute),
?int $second = (current second),
?int $month = (current month),
?int $day = (current day),
?int $year = (current year)
): int|falseThe first argument, $hour, is required since PHP 8.0 — calling mktime() with no arguments throws an ArgumentCountError. Any argument you omit defaults to the corresponding part of the current local date and time. The function returns the timestamp as an integer, or false if the arguments produce a date outside the valid range.
A Unix timestamp is the number of seconds elapsed since the Unix epoch — January 1, 1970, 00:00:00 UTC. It is the common currency for dates across PHP, databases, and operating systems.
Mind the argument order.
mktime()takes time before date:hour, minute, second, month, day, year. This is different from how we usually write dates (year-month-day), so it is the most common source of bugs.
Building a specific timestamp
To represent a fixed moment, pass all six parts. Here we build 2:30 PM on June 15, 2024:
We set the timezone explicitly so the output is reproducible. Without date_default_timezone_set(), mktime() interprets the parts in the server's configured timezone, which can shift the resulting timestamp.
Date math with mktime()
Because a timestamp is just a number of seconds, you can add or subtract seconds to move a date forward or backward. To find the date 30 days from a given day, add 30 × 86400 seconds (there are 86,400 seconds in a day):
Adding 30 days to a date
This raw-seconds approach is fine for whole days, but it ignores daylight-saving transitions: a "day" is not always exactly 86,400 seconds. For correct calendar arithmetic across DST boundaries, use DateTime::modify() or DateInterval instead.
Automatic normalization
A handy feature of mktime() is that it normalizes out-of-range values: it rolls them over into the correct date instead of failing. Pass month 13 and you get January of the next year; pass day 0 and you get the last day of the previous month:
This makes mktime() convenient for "the last day of this month" style calculations: mktime(0, 0, 0, $month + 1, 0, $year) gives the final day of $month. Note that this leniency also means mktime() will not reject a clearly invalid date like February 30 — it silently rolls it over. If you need to validate a calendar date, use checkdate() first.
Common pitfalls
- Wrong argument order. Writing
mktime(2024, 6, 15)thinking it is year-month-day produces a nonsensical date. Remember: time first, then month, day, year. - No arguments in PHP 8.
mktime()with zero arguments throwsArgumentCountError. To get the current timestamp, usetime()instead. - Two-digit years. Pass a full four-digit year. Values like
0–69map to 2000–2069 and70–100to 1970–2000, which is rarely what you want.
mktime() vs. the DateTime classes
mktime() is procedural and works in the local timezone. For UTC parts, use its sibling gmmktime(). For new code, prefer the object-oriented DateTime and DateTimeImmutable classes — they carry timezone information, handle DST-aware arithmetic, and avoid manual second math:
Conclusion
mktime() builds a Unix timestamp from date and time parts, automatically normalizing out-of-range values — which makes it handy for quick date math and "last day of the month" tricks. Keep its quirks in mind: time comes before date in the argument list, and a no-argument call now errors (use time() for "now"). For DST-correct, timezone-aware work in modern PHP, reach for DateTimeImmutable and DateInterval instead. See also checkdate() for validation and strtotime() for parsing human-readable date strings.