$now = new DateTime(); $timestamp = $now->getTimeStamp(); echo $timestamp; // outputs something like "1627987144"
$datetime_str = "2021-09-01 13:45:00"; $datetime = new DateTime($datetime_str); $timestamp = $datetime->getTimeStamp(); echo $timestamp; // outputs something like "1630529100"
$timestamp = 1627987144; $datetime = DateTime::createFromFormat("U", $timestamp); echo $datetime->format("Y-m-d H:i:s"); // outputs "2021-08-03 20:25:44"The DateTime class is part of the PHP core library, so no separate package/library is needed to use it.