Home > Backend Development > PHP Tutorial > 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 a specified date and time

王林
Release: 2023-07-24 13:34:02
Original
1536 people have browsed it

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)
Copy after login

Parameter description:

  • hour: hour, value range is 0 to 23.
  • minute: The number of minutes, the value range is 0 to 59.
  • second: The number of seconds, the value range is 0 to 59.
  • month: month, the value range is 1 to 12.
  • day: Date, the value range is 1 to 31, depending on the specified month.
  • year: Year, the value range is 1970 to 2038.
  • is_dst: Optional parameter indicating whether to consider Daylight Saving Time. The default value is -1, which means automatic judgment.

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;
Copy after login

Execute the above code, the output result is:

1633046400
Copy after login

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;
Copy after login

Executing the above code, the output result is:

2022-01-01 00:00:00
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template