Home > Article > Backend Development > Use the PHP function "mktime" to create a UNIX timestamp based on a specified date and time
Use the PHP function "mktime" to create a UNIX timestamp based on the specified date and time
UNIX timestamp is a standard way to represent time in computer systems. It represents the beginning of January 1, 1970 The number of seconds since 00:00:00 UTC (Coordinated Universal Time). In PHP, we can use the "mktime" function to create a UNIX timestamp based on a specified date and time. This article will introduce how to use the "mktime" function and provide sample code.
The syntax of the "mktime" function is as follows:
mktime(hour, minute, second, month, day, year, is_dst)
Parameter description:
The following is an example showing how to use the "mktime" function to create a UNIX timestamp:
$timestamp = mktime(12, 0, 0, 10, 1, 2021); echo $timestamp;
Execute the above code, the output result is:
1633046400
The above code , we created a UNIX timestamp representing 12:00:00 on October 1, 2021 through the "mktime" function.
The "mktime" function can also be used with PHP's date and time functions. For example, the "date" function can format UNIX timestamps into more readable dates and times:
$timestamp = mktime(0, 0, 0, 1, 1, 2022); $date = date("Y-m-d H:i:s", $timestamp); echo $date;
Executing the above code, the output result is:
2022-01-01 00:00:00
In the above code, we use the "mktime" function to create a UNIX timestamp representing 00:00:00 on January 1, 2022, and use "date" The function formats a UNIX timestamp into the string format of "Y-m-d H:i:s".
It should be noted that the "mktime" function will automatically complete illegal values when processing dates and times. For example, if the specified month is greater than 12, the "mktime" function will convert it to the corresponding month of the next year. Likewise, if the specified date is greater than the maximum date of the month, the "mktime" function will convert it to the corresponding date of the next month.
Summary:
This article introduces how to use the PHP function "mktime". By specifying the date and time, you can use the "mktime" function to create the corresponding UNIX timestamp. We also showed how to format a UNIX timestamp into a more readable string. I hope this article can help you successfully handle date and time related tasks in PHP development.
The above is the detailed content of Use the PHP function "mktime" to create a UNIX timestamp based on a specified date and time. For more information, please follow other related articles on the PHP Chinese website!