Home PHP Date/Time gmmktime() PHP gmmktime() Function 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.
$ts = gmmktime(22, 13, 20, 11, 14, 2023); Call What 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 gmmktime(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 UTC minute is used. second Optional. The second. If null, the current UTC second is used. month Optional. The month (1-12). Values outside the range roll over into the next or previous year. day Optional. The day of the month. Values outside the month's range roll over to the next or previous month. year Optional. The year. Values 0-69 map to 2000-2069 and 70-100 map to 1970-2000, so use four digits.
Return Value Return Value Description int The Unix timestamp for the given UTC date and time. false Returned on failure.
PHP Examples 1. Build a Timestamp from Parts 2. Values That Roll Over 3. Days Between Two Dates Example 1 — Build a Timestamp from Parts Give gmmktime() the time first, then the date.
<?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 The order is hour, minute, second, month, day, year. 22:13:20 on 14 November 2023 UTC is timestamp 1700000000. Midnight on 1 January 1970 is the epoch, so the result is 0. 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
$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 Month 13 of 2023 is the same as January 2024. Day 0 of March means the last day of February. 2024 is a leap year, so that last day is 29 February. gmdate() turns the timestamps back into readable dates. Frequently Asked Questions What is the difference between mktime() and gmmktime()? 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.
Why does gmmktime() take hour first? The order is a PHP legacy, inherited from the C mktime() function. It is hour, minute, second, month, day, year.
Can gmmktime() handle month 13 or day 0? 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.
Is gmmktime() affected by daylight saving time? No. UTC has no daylight saving time, so the result does not change with the default time zone.
Should I use gmmktime() or DateTimeImmutable? 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 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