PHP mktime() Function: Syntax, Example
In this tutorial, you will learn everything about the PHP mktime() function with its basic syntax and examples.
When you are working with date and time in PHP, it is often necessary to create timestamps manually for specific dates and times. For this purpose, PHP provides the mktime() function, which allows you to do exactly that.
PHP mktime() Function
The mktime() function in PHP is a built-in function which returns a Unix timestamp for a specific date and time. A Unix timestamp represents the number of seconds that have passed since the Unix Epoch (January 1, 1970, 00:00:00 GMT).
You can use this function when you want to calculate dates, compare times, or format custom date/time values using PHP’s date() function.
Syntax of mktime() Function in PHP
The basic syntax of mktime() function is as follows:
mktime(hour, minute, second, month, day, year, is_dst)
Parameters
The mktime() function accepts the following parameters:
- hour (Optional): Specifies the hour (0 to 23).
- minute (Optional): Specifies the minute (0 to 59).
- second (Optional): Specifies the second (0 to 59).
- month (Optional): Specifies the month (1 to 12).
- day (Optional): Specifies the day of the month (1 to 31).
- year (Optional) Specifies the year (e.g., 2025).
- is_dst (Optional) Used to set Daylight Saving Time. Default is -1 (auto).
Return Value
The mktime() function in PHP returns a Unix timestamp as an integer on success or false on failure.
Basic Example of PHP mktime() Function
Let’s take some practical examples based on the mktime() function provided by PHP.
Example 1:
<?php
$timestamp = mktime(14, 30, 0, 10, 7, 2025);
echo "Timestamp: " . $timestamp;
?>
Output:
Timestamp: 1765117800
In this example code, the output shows the Unix timestamp for October 7, 2025, 2:30:00 PM. To make the timestamp human-readable, use the date() function along with mktime().
Example 2: Convert Timestamp into Readable Date Format
<?php
$timestamp = mktime(14, 30, 0, 10, 7, 2025);
echo "Date and Time: " . date("Y-m-d H:i:s", $timestamp);
?>
Output:
Date and Time: 2025-10-07 14:30:00
In this example, we have created a timestamp using mktime() function for the specified date. Then, we have used the date() function to convert it into a readable format.
Example 3: Calculate Days Between Two Dates
You can use the mktime() function to find the number of days between two specific dates. Look at the below example code where we will calculate the number of days between January 1, 2025, and October 7, 2025.
<?php
$start = mktime(0, 0, 0, 1, 1, 2025);
$end = mktime(0, 0, 0, 10, 7, 2025);
$diff = ($end - $start) / (60 * 60 * 24);
echo "Days between: " . $diff;
?>
Output:
Days between: 279
Example 4: Handling Overflow Values
The mktime() function automatically adjusts overflow values. For example:
<?php
$date = date("Y-m-d", mktime(0, 0, 0, 14, 35, 2025));
echo "Adjusted date: " . $date;
?>
Output:
Adjusted date: 2026-03-07
Here, since the month value 14 exceeds 12, PHP automatically rolls over to the next year.
Example 5: Setting Expiry Dates
You can use PHP mktime() function to set cookie expiry or offer deadlines.
<?php
$expiry = mktime(0, 0, 0, date("m"), date("d") + 30, date("Y"));
echo "Offer expires on: " . date("Y-m-d", $expiry);
?>
Output:
Offer expires on: 2025-11-06
Key Takeaways about PHP mktime() Function
- The mktime() function creates a timestamp for a specific date and time.
- This function is useful for date calculations, such as yesterday, tomorrow, next month, etc.
- If you omit any argument in the mktime() function, PHP uses the current value for that unit.
- The mktime() function returns false if the date or time is invalid.
- Always check or format the timestamp using date() function for better readability.
- Use gmmktime() function if you need a GMT (UTC) based timestamp instead of the local timezone.