Home  >  Article  >  Backend Development  >  Let’s talk about php timestamp conversion function

Let’s talk about php timestamp conversion function

PHPz
PHPzOriginal
2023-03-29 10:11:59827browse

PHP Timestamp Conversion Function

Timestamp (timestamp) is a number that represents time, which can be used to represent the date from a fixed point in time (for example, January 1, 1970, Greenwich Mean Time) The number of seconds since day 0:00:00) to now. In web development, we often use timestamps. However, timestamps are generally expressed in 10-digit numbers, which is not user-friendly and difficult to read. This article will introduce some PHP timestamp conversion functions, so that we can easily convert timestamps into the date format we need.

1. Date() function

The date() function is a function used in PHP to format dates and times. It can also be used to convert timestamps into standard date formats. The date() function has two parameters. The first required parameter is the date format string, and the second optional parameter is the timestamp. If no timestamp is specified, the current time is used by default.

For example, to convert a timestamp into a standard date format (year-month-day hour:minute:second), you can use the following code:

$timestamp = 1567221493;  // 时间戳
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;  // 输出:2019-08-31 16:38:13

In the above code, change the time The stamp is passed to the date() function as the second parameter, and the first parameter is used to define the date format string. Y represents the 4-digit year (e.g. 2019), m represents the 2-digit month (e.g. 01), d represents the 2-digit date (e.g. 01), H represents the hour in the 24-hour format (e.g. 23), i represents the number of minutes (e.g. 59) and s represents the number of seconds (e.g. 00).

2. gmdate() function

The gmdate() function is very similar to the date() function. It can also be used to format date and time, but it will be based on Greenwich Mean Time ( GMT) to display date and time. Therefore, the gmdate() function can be used to convert the timestamp to GMT date format.

For example, to convert a timestamp into GMT date format, you can use the following code:

$timestamp = 1567221493;  // 时间戳
$date = gmdate('Y-m-d H:i:s', $timestamp);
echo $date;  // 输出:2019-08-31 08:38:13

In the above code, the timestamp is also passed as the second parameter to gmdate( ) function, the first parameter is used to define the date format string. Because the gmdate() function displays GMT time, the output time is 8 hours earlier than local time (that is, GMT 8).

3. strtotime() function

The strtotime() function can parse a text date string into a timestamp, or convert a timestamp into a date string in a specified format. This function returns the number of seconds since standard UNIX epoch (January 1, 1970).

For example, to convert a date string (such as 2019-08-31 16:38:13) into a timestamp, you can use the following code:

$date_str = '2019-08-31 16:38:13';  // 日期字符串
$timestamp = strtotime($date_str);
echo $timestamp;  // 输出:1567221493

In the above code, The date string is passed to the strtotime() function as the only parameter of the function, and the function will parse the date string into a timestamp.

4. DateTime class

DateTime is a class used to process dates and times in PHP. It provides a wealth of functions and methods. You can use the DateTime class to convert a timestamp into a date string in a specified format.

For example, to convert a timestamp into a standard date format, you can use the following code:

$timestamp = 1567221493;  // 时间戳
$date_time = new DateTime();
$date_time->setTimestamp($timestamp);
$date = $date_time->format('Y-m-d H:i:s');
echo $date;  // 输出:2019-08-31 16:38:13

In the above code, first create a DateTime object, and then set the time through the setTimestamp() method The stamp is set to the object, and finally the format() method is used to convert the object into a string in a standard date format.

5. Summary

The above introduces some commonly used timestamp conversion functions in PHP, including date() function, gmdate() function, strtotime() function and DateTime class. Using these functions, timestamps can be converted into date strings in any format, making it easier for us to process date formats in web development.

The above is the detailed content of Let’s talk about php timestamp conversion function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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