gmmktime()
What is PHP's gmmktime() Function?PHP's gmmktime() function is a built-in function that allows developers to generate a Unix timestamp for a specific date and
What is PHP's gmmktime() Function?
PHP's gmmktime() function builds a Unix timestamp from the individual parts of a date and time (hour, minute, second, month, day, year), interpreting those parts as GMT/UTC rather than the server's local timezone. A Unix timestamp is simply the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC — the value most PHP date functions use internally.
This page covers the function's syntax and parameters, how it differs from mktime(), how out-of-range values roll over, and the gotcha that bites most people: in modern PHP the parameters are not all optional.
The name reads as greenwich mean time + make time.
Syntax
gmmktime(
int $hour = current,
?int $minute = current,
?int $second = current,
?int $month = current,
?int $day = current,
?int $year = current
): int|falseIt returns the timestamp as an integer, or false if the arguments describe an invalid time.
Parameters
| Parameter | Meaning |
|---|---|
hour | Hours, 0–23 (required since PHP 8.0). |
minute | Minutes, 0–59. |
second | Seconds, 0–59. |
month | Month number, 1–12. |
day | Day of the month, 1–31. |
year | 4-digit year. |
Gotcha: In PHP 8.0 and later, calling
gmmktime()with no arguments throws anArgumentCountError—houris mandatory. Any argument you do omit defaults to the corresponding value of the current GMT time. To get "now" as a timestamp, usetime()instead.
Basic Example
This generates the Unix timestamp for March 3rd, 2023 at 12:30:00 PM GMT. To read it back in a human-friendly form, pair it with gmdate():
<?php
$timestamp = gmmktime(12, 30, 0, 3, 3, 2023);
echo gmdate('Y-m-d H:i:s', $timestamp);
// 2023-03-03 12:30:00gmmktime() vs. mktime()
gmmktime() and mktime() take the exact same arguments and return a Unix timestamp — the only difference is the timezone the parts are interpreted in. gmmktime() treats them as GMT/UTC; mktime() treats them as the server's local timezone. With the same inputs they produce different timestamps whenever local time is offset from UTC:
<?php
date_default_timezone_set('America/New_York'); // UTC-4 in June
echo gmmktime(12, 0, 0, 6, 15, 2023), "\n"; // 1686830400 (12:00 UTC)
echo mktime(12, 0, 0, 6, 15, 2023), "\n"; // 1686844800 (12:00 New York = 16:00 UTC)Use gmmktime() when your inputs are already in UTC (timestamps from an API, a database stored in UTC, etc.) so the result doesn't shift with the server's timezone setting.
Out-of-Range Values Roll Over
You don't have to keep the parts inside their normal ranges. gmmktime() normalizes them, which is handy for date arithmetic without manual carrying:
<?php
// Month 13 rolls into the next year
echo gmdate('Y-m-d', gmmktime(0, 0, 0, 13, 1, 2023)), "\n"; // 2024-01-01
// Day 32 of January becomes February 1st
echo gmdate('Y-m-d', gmmktime(0, 0, 0, 1, 32, 2023)), "\n"; // 2023-02-01
// Hour 25 rolls into the next day at 01:00
echo gmdate('Y-m-d H:i', gmmktime(25, 0, 0, 1, 1, 2023)), "\n"; // 2023-01-02 01:00This makes expressions like gmmktime(0, 0, 0, $month, $day + 7, $year) ("seven days later") work correctly across month and year boundaries.
When to Use gmmktime()
- Timezone consistency. Because it always uses UTC, the same arguments produce the same timestamp on every server, regardless of
date.timezone. - Building timestamps from parts. When you have separate hour/day/month values (e.g. from a form or parsed string) rather than a single date string. For a full date string such as
"2023-03-03 12:30", reach forstrtotime()instead. - Date math via rollover. Add or subtract from a component and let the function normalize the result.
If you only need to validate that a month/day/year combination is a real calendar date, see checkdate().
Conclusion
gmmktime() turns separate date and time components into a UTC-based Unix timestamp. Its key strengths are timezone independence and automatic normalization of out-of-range values. Remember that, unlike older PHP versions, PHP 8+ requires at least the hour argument, and reach for mktime() when your inputs are in local time and gmdate() to format the result.