In PHP, converting a timestamp to a date is a common operation. Typically, this is done using the built-in date() function, which takes two parameters: a date format string and a timestamp, and returns the corresponding formatted date.
date(format, timestamp)
Where:
Here are some common date format specifiers:
Specifier | Output Format |
---|---|
Y | 4-digit year |
m | 2-digit month |
d | 2-digit day |
H | 2-digit hour (24-hour format) |
i | 2-digit minute |
s | 2-digit second |
Here’s an example showing how to convert the timestamp 1658096324 into a date:
$timestamp = 1658096324;
$date = date('Y-m-d H:i:s', $timestamp);
echo $date;
2022-07-19 14:58:44
When using the date() function, ensure that the timestamp is a valid Unix timestamp. If the timestamp is invalid, date() will return FALSE.
Through this guide, you now know how to convert timestamps to dates in PHP. We hope this article helps you. For more in-depth PHP tutorials, be sure to check out more related content.