Convert timestamp to DateTime in PHP
Question:
How to convert timestamp to DateTime in PHP? Answer:
$date = new DateTime();
$date->setTimestamp($timestamp);
echo $date->format('Y-m-d');
Description:
To convert a Unix timestamp to a valid PHP date you can use the DateTime
class. First, create a simple DateTime
object, then set its value using the setTimestamp
method. After that, you can get your date in any format using the format
method.
Reference:
DateTime setTimestamp reference
Share "How to convert timestamp to DateTime in PHP?"
Related snippets:
- Add minutes to date time in PHP
- Add days to date in PHP
- Get same day in the next week in PHP
- Get time difference in minutes in PHP
- Convert date to timestamp in PHP
- Convert timestamp to DateTime in PHP
- Get the last day of a month from date in PHP
- Get the number of days between two dates in PHP
- Get yesterday's date in PHP
- Get tomorrow's date in PHP
- Get current time in PHP
- Get actual date in PHP
Tags:
timestamp to datetime, Unix timestamp to PHP date Technical term:
Convert timestamp to DateTime in PHP