Home > Article > Backend Development > Convert php numeric timestamp to date and time
PHP Convert numeric timestamp to date and time: Use the date() function to convert a timestamp to a date and time in a specific format. Use the gmdate() function to convert a timestamp to a date and time in Greenwich Mean Time (GMT) format. Additional format specifiers are supported, such as d (date), m (month), Y (year), H (hour), i (minute), and s (second).
Convert PHP numeric timestamp to date and time
In PHP, a numeric timestamp represents the UTC epoch (1970 The number of seconds since midnight on January 1, year). To convert a numeric timestamp to a date and time, you can use the following function:
date()
Syntax:
date(format, timestamp)
Where:
Example:
$timestamp = 1653433200; $date = date('Y-m-d H:i:s', $timestamp); echo $date; // 输出:2022-05-25 12:00:00
gmdate()
Syntax:
gmdate(format, timestamp)
The gmdate() function is similar to the date() function, but it uses Greenwich Mean Time (GMT) instead of local time.
Example:
$timestamp = 1653433200; $date = gmdate('Y-m-d H:i:s', $timestamp); echo $date; // 输出:2022-05-25 07:00:00
Demo Example
Suppose we have a database field that stores a timestamp. To get the date and time from this field we can use the following code:
$timestamp = $row['timestamp']; $date = date('Y-m-d H:i:s', $timestamp); echo $date; // 输出:日期和时间
Other format specifiers
In addition to the above formats, the date() function also supports Other format specifiers, including:
The above is the detailed content of Convert php numeric timestamp to date and time. For more information, please follow other related articles on the PHP Chinese website!