PHP gmmktime() Function

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

Definition

In PHP, gmmktime() builds a Unix timestamp from a date and time that you give in GMT (UTC).

It is the UTC version of mktime(). You pass the hour, minute, second, month, day and year, and PHP returns the number of seconds since 1 January 1970 UTC.

Syntax

Call gmmktime() with the values you need. Start with this line.

PHP
$ts = gmmktime(22, 13, 20, 11, 14, 2023);
CallWhat it does
gmmktime(22, 13, 20, 11, 14, 2023)Returns 1700000000.
gmmktime(0, 0, 0, 1, 1, 1970)Returns 0, the Unix epoch.
gmmktime(0, 0, 0, 1, 1, 2000)Returns 946684800.

The arguments go from the smallest unit to the largest in the time part (hour, minute, second), then month, day, year. This order is unusual, so double-check it. Any argument you leave out uses the current UTC value.

Library declarations
PHP
gmmktime(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 UTC minute is used.
secondOptional. The second. If null, the current UTC second is used.
monthOptional. The month (1-12). Values outside the range roll over into the next or previous year.
dayOptional. The day of the month. Values outside the month's range roll over to the next or previous month.
yearOptional. The year. Values 0-69 map to 2000-2069 and 70-100 map to 1970-2000, so use four digits.

Return Value

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

PHP Examples

Example 1 — Build a Timestamp from Parts

Give gmmktime() the time first, then the date.

PHP
<?php
echo gmmktime(22, 13, 20, 11, 14, 2023), PHP_EOL;
echo gmmktime(0, 0, 0, 1, 1, 1970), PHP_EOL;
echo gmmktime(0, 0, 0, 1, 1, 2000), PHP_EOL;

How It Works

  1. The order is hour, minute, second, month, day, year.
  2. 22:13:20 on 14 November 2023 UTC is timestamp 1700000000.
  3. Midnight on 1 January 1970 is the epoch, so the result is 0.
  4. Midnight on 1 January 2000 UTC is 946,684,800 seconds after the epoch.

Example 2 — Values That Roll Over

gmmktime() does not complain about month 13 or day 0. It just rolls over to the right date.

PHP
<?php
$a = gmmktime(0, 0, 0, 13, 1, 2023);
$b = gmmktime(0, 0, 0, 3, 0, 2024);

echo gmdate('Y-m-d', $a), PHP_EOL;
echo gmdate('Y-m-d', $b), PHP_EOL;

How It Works

  1. Month 13 of 2023 is the same as January 2024.
  2. Day 0 of March means the last day of February.
  3. 2024 is a leap year, so that last day is 29 February.
  4. gmdate() turns the timestamps back into readable dates.

Example 3 — Days Between Two Dates

Subtract two UTC timestamps and divide by the seconds in a day.

PHP
<?php
$start = gmmktime(0, 0, 0, 11, 14, 2023);
$end   = gmmktime(0, 0, 0, 12, 25, 2023);

echo 'Days: ', ($end - $start) / 86400, PHP_EOL;

How It Works

  1. Both timestamps are at midnight UTC, so no daylight saving shifts can appear.
  2. Subtracting gives the gap in seconds.
  3. One day has 86,400 seconds, so dividing gives the number of days.
  4. From 14 November to 25 December is 41 days.

Frequently Asked Questions

mktime() reads the date as local time in the default time zone, while gmmktime() reads it as UTC. With the UTC time zone, both return the same value.
The order is a PHP legacy, inherited from the C mktime() function. It is hour, minute, second, month, day, year.
Yes. Out-of-range values roll over. Month 13 becomes January of the next year, and day 0 becomes the last day of the previous month.
No. UTC has no daylight saving time, so the result does not change with the default time zone.
For simple UTC timestamps, gmmktime() is fine. For anything with time zones or date math, DateTimeImmutable is usually clearer and safer.

Next: gmstrftime()

gmstrftime() formats a timestamp as a GMT (UTC) string using C-style % format codes. It is.

gmstrftime() 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