Display UTCDateTime in PHP

I have a returned query, with documents that contain MongoDB dates.

If I simply echo the $document['dateField'], it displays as:

echo "Date Played: ".$round['roundDate']."<br>";

Date Played: 1620255600000

I would prefer this as:

Date Played: 15/04/2021


Date Played: 15th April 2021

I have tried, which wants an int for the 2nd parameter:

echo "Date Played: ".date("DD/MM/YYYY", $round['roundDate'])."<br>";

But this errors with:

Fatal error : Uncaught TypeError: date(): Argument #2 ($timestamp) must be of type ?int, MongoDB\BSON\UTCDateTime given in /var/www/html/selectComp.php:39 Stack trace: #0 /var/www/html/selectComp.php(39): date(‘DD/MM/YYYY’, Object(MongoDB\BSON\UTCDateTime)) #1 {main} thrown in /var/www/html/selectComp.php on line 39

I know this is just a date conversion, but it always confuses me…

Fixed with (but not played with other format styles / options):

$round['roundDate']->toDateTime()->format("d M Y");

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.