Home PHP Date/Time mktime() PHP mktime() Function Definition In PHP, mktime() builds a Unix timestamp from a date and time that you give in the default (local) time zone.
You pass the hour, minute, second, month, day and year, and PHP returns the number of seconds since 1 January 1970 UTC. It is one of the simplest ways to turn separate numbers into a timestamp.
Syntax Call mktime() with the values you need. Start with this line.
$ts = mktime(22, 13, 20, 11, 14, 2023); Call What it does mktime(22, 13, 20, 11, 14, 2023)Returns 1700000000 when the time zone is UTC. mktime(0, 0, 0, 1, 1, 1970)Returns 0 when the time zone is UTC. mktime(0, 0, 0, 3, 0, 2024)Returns the timestamp for 29 February 2024, the last day of February.
The argument order is hour, minute, second, month, day, year. This is easy to mix up with the usual year-month-day order. If you want UTC no matter the default time zone, use gmmktime() instead.
Library declarations mktime(int $hour, ?int $minute = null, ?int $second = null, ?int $month = null, ?int $day = null, ?int $year = null): int|false Parameters Parameter Description hour Required. The hour. Values outside 0-23 roll over into the next or previous day. minute Optional. The minute. If null, the current minute is used. second Optional. The second. If null, the current second is used. month Optional. The month (1-12). Values outside the range roll over into other years. day Optional. The day of the month. Values outside the month's range roll over into other months. year Optional. The year. Use four digits. Values 0-69 map to 2000-2069 and 70-100 map to 1970-2000.
Return Value Return Value Description int The Unix timestamp for the given local date and time. false Returned on failure.
PHP Examples 1. Make a Timestamp 2. Rollover and Last Day of the Month 3. The Same Date in Two Time Zones Example 1 — Make a Timestamp Build timestamps from separate numbers, then format one back into text.
<?php
date_default_timezone_set('UTC');
echo mktime(22, 13, 20, 11, 14, 2023), PHP_EOL;
echo mktime(0, 0, 0, 1, 1, 1970), PHP_EOL;
echo date('Y-m-d H:i:s', mktime(0, 0, 0, 12, 25, 2023)), PHP_EOL; 1700000000
0
2023-12-25 00:00:00 How It Works The time zone is UTC, so the result is the same on every server. 22:13:20 on 14 November 2023 is timestamp 1700000000. Midnight on 1 January 1970 is the epoch, so the result is 0. date() formats a timestamp back into a readable string. Example 2 — Rollover and Last Day of the Month Out-of-range values do not fail. They roll into the next or previous period.
<?php
date_default_timezone_set('UTC');
echo date('Y-m-d', mktime(0, 0, 0, 13, 1, 2023)), PHP_EOL;
echo date('Y-m-d', mktime(0, 0, 0, 2, 30, 2024)), PHP_EOL;
echo date('Y-m-d', mktime(0, 0, 0, 3, 0, 2024)), PHP_EOL; 2024-01-01
2024-03-01
2024-02-29 How It Works Month 13 of 2023 becomes January 2024. 30 February 2024 does not exist. 2024 has 29 days in February, so it becomes 1 March.Day 0 of March means the last day of February, which is a handy trick. The last day of February 2024 is 29 because 2024 is a leap year. Frequently Asked Questions What does mktime() do in PHP? It builds a Unix timestamp from the hour, minute, second, month, day and year, read in the default time zone.
What is the argument order of mktime()? Hour, minute, second, month, day, year. It is not the usual year-month-day order, so check it carefully.
How do I get the last day of a month with mktime()? Ask for day 0 of the next month. For example, mktime(0, 0, 0, 3, 0, 2024) is 29 February 2024.
What is the difference between mktime() and gmmktime()? mktime() reads your numbers in the default time zone, while gmmktime() reads them as UTC.
Should I use mktime() or DateTimeImmutable? mktime() is fine for quick timestamps. For time zones, date math and clear code, DateTimeImmutable is usually the better choice.
Next: strftime() strftime() formats a timestamp in local time using C-style % format codes. It is deprecate.
strftime() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion