The mktime() function works to create a Unix timestamp based on the given time and date parameters. In other words, it is often used to determine the number of seconds that have elapsed since the Unix epoch (January 1, 1970, 00:00:00 GMT), thereby providing a timestamp value that can be used for various purposes in a PHP script.
Syntax of the mktime() Function
First, we must understand the basic syntax of the mktime() function. The function takes six arguments – hour, minute, second, month, day, and year – and returns a Unix timestamp. The format for calling the mktime() function is as follows:
For example, the following code snippet will return a timestamp representing May 19, 2023, 12:00:00 AM:
Importance of Unix Timestamp
This timestamp provides a standard for representing date and time in a numerical format that is easily manipulated by computer programs. It is important to remember that the Gregorian calendar, which is the calendar used in the majority of the globe, provides the basis for the Unix timestamp. This means that the mktime() function can handle dates and times that fall within the range of January 1, 1970 00:00:00 GMT to January 19, 2038 03:14:07 GMT.
How to Use PHP mktime() Function
The mktime() function can be used for a variety of purposes in a PHP script.
- Calculate the Time Difference
- Generate Timestamp
- Convert a Date and Time String into Timestamp
- Date Arithmetic
- Handle Dates and Times in Other Time Zones
1: Calculate the Time Difference
It can be used to calculate the difference between two dates or times. To do this, subtract one timestamp from another, resulting in a value that represents the duration in seconds between the two timestamps. The following code snippet demonstrates how to calculate the number of seconds between May 10, 2023, 12:00:00 AM and May 19, 2023, 12:00:00 AM:
$timestamp1 = mktime(0, 0, 0, 5, 10, 2023);
$timestamp2 = mktime(0, 0, 0, 5, 19, 2023);
$difference = $timestamp2 - $timestamp1;
echo $difference;
?>
For each of the two dates, a Unix timestamp is generated using the mktime() function. The timestamp for May 10, 2023, is subtracted from the timestamp for May 19, 2023, to determine the difference between the two timestamps.
The echo command is then used to display the calculation’s result, which is the difference in seconds between the two timestamps.
2: Generate Timestamp
To create a timestamp for the current date and time, use the mktime() method. The justifications for hour, minute, second, month, day, and year are left out to achieve this. The following code snippet demonstrates how to generate a timestamp for the current date and time:
date_default_timezone_set('Asia/Karachi');
$date_str = date('Y-m-d H:i:s');
$current_timestamp = mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y'));
echo "The current Unix timestamp is: $current_timestamp";
?>
In this example, we first use the date_default_timezone_set() method to set the default time zone to Asia/Karachi. Then, we create a formatted date string for the current date and time in the chosen time zone using the date() method. The year, month, day, hour, minute, and second are represented by the characters Y-m-d H:i:s in the date string.
The mktime() function is then used to create a Unix timestamp for the given day and time. The month, day, and year are sent in as parameters along with the hour, minute, and second, which we obtain from the formatted date string using the date() method.
Finally, we print the most recent Unix timestamp on the console.
3: Convert a Date and Time String into Timestamp
The mktime() function is also useful for converting a date and time string into a timestamp. This can be achieved by using the date_parse() function to extract the individual components of the date and time string, then passing those components as arguments to the mktime() function.
The following code snippet demonstrates how to convert the date and time string May 19, 2023, 12:00:00 AM into a timestamp:
$date_string = "May 19, 2023 12:00:00 AM";
$date_components = date_parse($date_string);
$timestamp = mktime($date_components['hour'], $date_components['minute'], $date_components['second'], $date_components['month'], $date_components['day'], $date_components['year']);
echo $timestamp;
?>
A string corresponding to a date and time is contained in the $date_string variable. This string is broken down into its component parts, including the hour, minute, second, month, day, and year, using the date_parse() function
The components of the date string are then created into a Unix timestamp using the mktime() function. The mktime() function returns a Unix timestamp that represents the given date and time after taking as inputs the hour, minute, second, month, day, and year.
The echo statement is then used to display the Unix timestamp.
4: Date Arithmetic
Calculating dates via mathematical operations is another common application of mktime(). Additions or subtractions to a specific date can be quickly and accurately calculated using mktime(). The following code adds two days to the current date:
$date = mktime(0, 0, 0, date("m"), date("d") + 2, date("Y"));
echo "Date after adding 2 days: " . date("m-d-Y", $date);
?>
A Unix timestamp for the time and date that is two days from the current time is produced using the mktime() function. The generated date and time are then formatted using the m-d-Y (month-day-year) format using the date() method. The echo statement is then used to display the formatted date.
5: Handle Dates and Times in Other Time Zones
The mktime() function can also handle dates and times in time zones other than GMT. This is achieved by setting the time zone using the date_default_timezone_set() function before calling the mktime() function.
The following code snippet demonstrates how to generate a timestamp for May 19, 2023, 12:00:00 AM in the Pacific Time Zone:
date_default_timezone_set("America/Los_Angeles");
$timestamp = mktime(0, 0, 0, 5, 19, 2023);
echo $timestamp;
?>
The default time zone is set to America/Los_Angeles using the date_default_timezone_set() method. All date and time functions in the PHP script use the time zone that is established by this function.
Then, a Unix timestamp for May 19, 2023, at midnight in the America/Los_Angeles time zone is generated using the mktime() function.
Conclusion
The mktime() is a well-known PHP function that is used to convert date and time information into Unix timestamps. It has several uses such as calculating time differences, generating a timestamp, performing arithmetic operations on dates, and much more.