What is PHP mktime() function?
If you want to get number of seconds between UNIX Epoch (1st January 1970 00:00:00 GMT) and a particular date and time, use PHP mktime() function. Example 1000180800. You can make this large seconds human readable format later though.
You can use 8 different parts of a date to mention a particular date and time inside the function – hour, minute, second, month, day, year.
Syntax:
mktme(hour, minute, second, month, day, year)
Parameters:
hour (Required): The number of hour of the specified date and time. Its value is between 0-23.
- A negative hour indicates the previous day.
- An hour greater than 23 indicates the next day.
minute (Optional): The number of minute of the specified date and time. Its value is between 0-59.
- A negative minute indicates the previous hour.
- A minute greater than 23 indicates the next hour.
second (Optional): The number of second of the specified date and time. Its value is between 0-59.
- A negative second indicates the previous minute.
- A second greater than 23 indicates the next minute.
month (Optional): The number of month of the specified date and time. Its value is between 1-12.
- Values less than 1 indicates the month of previous year in reverse order like 0 means December, 1 means November etc.
- Value greater than 12 indicates the month of the next year like 13 means January, 14 means February etc.
day (Optional): The number of day of the specified date and time. Its value is between 1-31 (depends on a particular month ex. 31 for January, 30 for November etc.).
- Values less than 1 indicates the days of previous month in reverse order like 0 means last day of the previous month, -1 means previous day of the last day of the previous month etc.
- Value greater than the specific month number indicates the next month like 32 for the January is 1st February, 33 for June is 2nd July etc.
year (Optional): The number of year of the specified date and time. It could be a 2-digit or 4-digit long. The 2-digit value 0-69 indicates 2000-2069 and The 2-digit value 70-100 indicates 1970-2000. Check example 8.
Return Values:
The function returns UNIX timestamp that it generates from the provided arguments.
Examples:
Example 1: Finding day of a date with mktime() function-
<?php
$timestamp = mktime(0, 0, 0, 9, 11, 2001);
echo "9/11 2001 was on " . date("l", $timestamp);
?>
Output:
Explanation:
9/11 2001 was on Tuesday
Explanation:
The mktime() function at line 2 generates timestamp of the date 9/11 and the date() function in the next line displays the full day name of the month by the “l” formatting character.
Example 2: Calculating date with mktime() function that has 24 as hours and 32 as day-
<?php
echo date("m-d-Y g:i:s a", mktime(00, 00, 00, 01, 01, 2024));
echo "<br />";
echo date("m-d-Y g:i:s a", mktime(24, 00, 00, 12, 31, 2023));
echo "<br />";
echo date("m-d-Y g:i:s a", mktime(00, 00, 00, 01, 01, 2024));
echo "<br />";
echo date("m-d-Y g:i:s a", mktime(00, 00, 00, 12, 32, 2023));
echo "<br />";
?>
Output:
01-01-2024 12:00:00 am
01-01-2024 12:00:00 am
01-01-2024 12:00:00 am
01-01-2024 12:00:00 am
Explanation:
When mktime() function finds “24” as hour value at line 4, it indicates the first hour of the next day which is the same as the line no 2.
When mktime() function finds “32” as day number of the month December (4th parameter) value at line 8, it considers the 1st (32-31) day of the next month. So, both mktime() at line 6 and 8 generates same output.
Example 3: Calculating date with mktime() function that has negative hour and negative day-
<?php
echo date("m-d-Y g:i:s a", mktime(23, 59, 59, 12, 31, 2023));
echo "<br />";
echo date("m-d-Y g:i:s a", mktime(-01, 59, 59, 01, 01, 2024));
echo "<br />";
echo date("m-d-Y g:i:s a", mktime(23, 59, 59, 12, 30, 2023));
echo "<br />";
echo date("m-d-Y g:i:s a", mktime(23, 59, 59, 01, -01, 2024));
echo "<br />";
?>
Output:
12-31-2023 11:59:59 pm
12-31-2023 11:59:59 pm
12-30-2023 11:59:59 pm
12-30-2023 11:59:59 pm
Explanation:
When mktime() function finds “-01” as hour value at line 4, it considers it as the 1 hour earlier of the previous day which is the same as the line no 2.
When mktime() function finds “-01” as day value at line 8, it considers it as the last day of the previous month which is the same as the line no 6.
Example 4: Comparing two different dates with mktime() function-
<?php
$timestamp1 = mktime(23, 59, 59, 12, 31, 2023);
$timestamp2 = mktime(-01, 59, 59, 01, 01, 2024);
if($timestamp1 > $timestamp2){
echo date("m-d-Y g:i:s a", $timestamp2) . " is earlier than " . date("m-d-Y g:i:s a", $timestamp1);
}elseif($timestamp2 > $timestamp1){
echo date("m-d-Y g:i:s a", $timestamp1) . " is earlier than " . date("m-d-Y g:i:s a", $timestamp2);
}elseif($timestamp1 == $timestamp2){
echo "Both date " . date("m-d-Y g:i:s a", $timestamp1) . " and " . date("m-d-Y g:i:s a", $timestamp2) . " are same.";
}
?>
Output:
Both date 12-31-2023 11:59:59 pm and 12-31-2023 11:59:59 pm are same.
Explanation:
The parameters of the both mktime() functions at line 2 and 3 indicates the same date and time. So they generate same timestamp and results the output above.
Example 5: Calculating time difference with mktime() function-
<?php
$timestamp1 = mktime(06, 00, 00, 01, 01, 2024);
$timestamp2 = mktime(15, 55, 00, 01, 01, 2024);
$timeDifference = $timestamp2 - $timestamp1;
echo "Difference is: " . $timeDifference . " seconds.";
?>
Output:
Difference is: 35700 seconds.
Example 6: Omitting parameters of mktime() function-
<?php
echo date("m-j-Y g:i:s a", mktime(23, 50, 41, 12, 30, 2023));
echo "<br />";
echo date("m-j-Y g:i:s a", mktime(23, 50, 41, 12, 30));
echo "<br />";
echo date("m-j-Y g:i:s a", mktime(23, 50, 41, 12));
echo "<br />";
?>
Output:
12-30-2023 11:50:41 pm
12-30-2023 11:50:41 pm
12-30-2023 11:50:41 pm
Explanation:
We omit a parameter from the mktime() function, the function takes the parameter from the server. The first mktime() function uses the local server’s month (30) and year (2023). So, when we omit the year (2023) in the next mktime() function at line 4 and omit the year (2023) and day (30) in the mktime() function at line 6, the mktime() function takes the these missing parameters from server. So all the 3 mktime() functions displays same result.
Example 7: Omitting all the parameters of mktime() function-
<?php
echo date("m-j-Y g:i:s a", mktime(23));
echo "<br />";
echo date("m-j-Y g:i:s a", mktime());
?>
Output:
12-30-2023 11:03:49 pm
Fatal error: Uncaught ArgumentCountError: mktime() expects at least 1 argument, 0 given in D:\xampp\htdocs\mktime.php:62 Stack trace: #0 D:\xampp\htdocs\mktime.php(62): mktime() #1 {main} thrown in D:\xampp\htdocs\mktime.php on line 62
Explanation:
In line 2, the mktime() function has only 1 parameter and the function considers it as year. And, it takes the remaining 7 parameters from the server’s date and time.
As the function must have at least one parameter, so the mktime() function returns fatal error in line 4.
Example 8: Generating seconds (timestamp) with 2-digit year in mktime() function-
<?php
echo date("m-d-Y", mktime(0, 0, 0, 01, 01, 69));
echo "<br />";
echo date("m-d-Y", mktime(0, 0, 0, 01, 01, 2069));
echo "<br />";
echo date("m-d-Y", mktime(0, 0, 0, 01, 01, 1969));
?>
Output:
01-01-2069
01-01-2069
01-01-1969
Explanation:
The mktime() function considers 2-digit year 69 as 2069 not 1969. That’s why line 2 prints the year as 2069.
Practical Usages of mktime() Function:
You can do lots of date and time operations with this mktime() function. Few including-
- Finding day of a date. Check example 1.
- Comparing two different dates. Check example 4.
- Calculating time difference. Check example 5.
Notes on mktime() Function:
- If one or more parameter is omitted, the function starts to cut the parameters from the end of the function. For example, if one parameter is omitted, the function considers “year” as the missing parameter. if two parameters are omitted, the function considers “year” and “day” as the missing parameters. Check example 6.
- You can omit all the parameters except the first one, hour. If you discard this, you’ll get a fatal error. Check example 7.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP mktime() Function
mktme() is one of the very useful PHP date and time functions. When you get timestamp for a specific date using mktime() function, you can do lots of date and time operation with it.