Home >Backend Development >PHP Problem >How to convert timestamp to time format in php
A timestamp is a way to represent time, it is the number of seconds since 00:00:00 on January 1, 1970. In many applications, we need to convert timestamps into human-readable time formats for a clearer understanding of time. There are several simple ways to convert timestamp to time in PHP. In this article, we will explore these methods.
Method 1: Use the date() function
The date() function can convert the timestamp into a human-readable time format. The following is the parameter format of the date() function:
date(format, timestamp);
Among them, format is the format parameter and timestamp is the timestamp. format can contain the following types of characters:
For example, the following code will timestamp Convert to "month/day/year hour:minute:second am/pm" format:
$timestamp = time();
$date = date('m/d/Y h:i: s a', $timestamp);
echo $date;
Output: 07/28/2021 11:53:45 am
Method 2: Use strftime() function
The strftime() function is used to format local dates and times, and can convert timestamps into human-readable time formats. The following is the parameter format of the strftime() function:
strftime(format, timestamp);
Same as the date() function, format and timestamp are function parameters. The format contains the following types of characters:
For example, the following code converts the timestamp to the "month/day/year hour:minute:second am/pm" format:
$timestamp = time();
$date = strftime('%m/%d/%Y %l:%M:%S %p', $timestamp);
echo $ date;
Output: 07/28/2021 11:53:45 am
Method 3: Using the DateTime class
The DateTime class in PHP provides a simple interface to handle dates and times. The createFromFormat() method in the DateTime class can convert the timestamp into a human-readable time format.
The following is the parameter format of the createFromFormat() method:
createFromFormat(format, timestamp);
Among them, format is the string format, if it needs to be converted to the time format If it is different from the input time format, you need to specify the string format. If you need to convert the timestamp into the standard "year-month-day" format, the format should be "Y-m-d H:i:s". timestamp is the timestamp to be converted.
For example, the following code uses the createFromFormat() method to convert a timestamp to a standard time format:
$timestamp = time();
$date = DateTime::createFromFormat('Y-m-d H:i:s', date('Y-m-d H:i:s', $timestamp));
echo $date->format('Y-m-d H:i:s');
Output: 2021-07-28 11:53:45
Conclusion
In this article, we introduced three methods to convert timestamps into human-readable time formats. We can use the date() function and strftime() function in PHP, or use the DateTime class in PHP. Choose the appropriate method for handling timestamp conversions based on your application's needs and personal preferences.
The above is the detailed content of How to convert timestamp to time format in php. For more information, please follow other related articles on the PHP Chinese website!