Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Date and Time

PHP mktime() function

Reference for PHP mktime() and gmmktime(): build a Unix timestamp from hour, minute, second, month, day and year, with overflow rules and time zone notes.

mktime() builds a Unix timestamp from separate date and time parts. It is the reverse of date(): instead of formatting a timestamp, you give the hour, minute, second, month, day and year, and get the integer back. The parts are interpreted in the default time zone; gmmktime() interprets them as UTC.

Note the unusual argument order: time first (hour, minute, second), then month, day, year. Values out of range do not cause an error. They roll over into the next unit, which makes mktime() handy for simple calendar arithmetic.

PHP
<?php
echo mktime(0, 0, 0, 1, 1, 1970);   // 0, when the time zone is UTC

Try it

PHP Building timestamps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Overflow is a feature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
mktime(
    int $hour,
    ?int $minute = null,
    ?int $second = null,
    ?int $month = null,
    ?int $day = null,
    ?int $year = null
): int|false
gmmktime(...same parameters...): int|false
Name Description
$hour Hour, 0 to 23. Required since PHP 8.0.
$minute, $second Optional. null means the current value.
$month, $day Optional. null means the current value.
$year Optional. null means the current year. Values 0 to 69 become 2000 to 2069, 70 to 100 become 1970 to 2000. Use four digits to avoid surprises.

Return value: the timestamp as an int. The false in the signature is a legacy of old PHP; with valid integers on 64-bit builds it does not occur.

Usage notes

  • Order matters. mktime($hour, $minute, $second, $month, $day, $year) is easy to mix with checkdate($month, $day, $year). Double-check the order.
  • No validation. mktime(0, 0, 0, 2, 30, 2025) quietly returns 2 March 2025. Call checkdate() when the input is not trusted.
  • Month arithmetic is naive. Adding one month to 31 January gives 3 March (or 2 March in a leap year). Use DateTimeImmutable with modify("last day of next month") for calendar-aware logic.
  • Daylight saving gaps. A wall-clock time that does not exist (the skipped hour in spring) is moved forward; an ambiguous one (the repeated hour in autumn) picks one of the two.
  • Time zone. The result depends on the default time zone. For a stable value use gmmktime() or pass a zone to DateTimeImmutable.
  • Calling mktime() with no arguments is an error since PHP 8.0; use time().