PHP mktime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

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.

PHP
$ts = mktime(22, 13, 20, 11, 14, 2023);
CallWhat 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
PHP
mktime(int $hour, ?int $minute = null, ?int $second = null, ?int $month = null, ?int $day = null, ?int $year = null): int|false

Parameters

ParameterDescription
hourRequired. The hour. Values outside 0-23 roll over into the next or previous day.
minuteOptional. The minute. If null, the current minute is used.
secondOptional. The second. If null, the current second is used.
monthOptional. The month (1-12). Values outside the range roll over into other years.
dayOptional. The day of the month. Values outside the month's range roll over into other months.
yearOptional. The year. Use four digits. Values 0-69 map to 2000-2069 and 70-100 map to 1970-2000.

Return Value

Return ValueDescription
intThe Unix timestamp for the given local date and time.
falseReturned on failure.

PHP Examples

Example 1 — Make a Timestamp

Build timestamps from separate numbers, then format one back into text.

PHP
<?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;

How It Works

  1. The time zone is UTC, so the result is the same on every server.
  2. 22:13:20 on 14 November 2023 is timestamp 1700000000.
  3. Midnight on 1 January 1970 is the epoch, so the result is 0.
  4. 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
<?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;

How It Works

  1. Month 13 of 2023 becomes January 2024.
  2. 30 February 2024 does not exist. 2024 has 29 days in February, so it becomes 1 March.
  3. Day 0 of March means the last day of February, which is a handy trick.
  4. The last day of February 2024 is 29 because 2024 is a leap year.

Example 3 — The Same Date in Two Time Zones

Midnight in India happens earlier than midnight in UTC, so the timestamps are different.

PHP
<?php
date_default_timezone_set('UTC');
echo 'UTC:          ', mktime(0, 0, 0, 1, 1, 2024), PHP_EOL;

date_default_timezone_set('Asia/Kolkata');
echo 'Asia/Kolkata: ', mktime(0, 0, 0, 1, 1, 2024), PHP_EOL;

How It Works

  1. The first call builds midnight on 1 January 2024 in UTC.
  2. Then the default time zone changes to Asia/Kolkata (UTC+5:30).
  3. Midnight in Kolkata comes 5 hours 30 minutes earlier than midnight in UTC.
  4. That is 19,800 seconds less, so the second timestamp is smaller.

Frequently Asked Questions

It builds a Unix timestamp from the hour, minute, second, month, day and year, read in the default time zone.
Hour, minute, second, month, day, year. It is not the usual year-month-day order, so check it carefully.
Ask for day 0 of the next month. For example, mktime(0, 0, 0, 3, 0, 2024) is 29 February 2024.
mktime() reads your numbers in the default time zone, while gmmktime() reads them as UTC.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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