MySQL datetime for echo in PHP

I want to show on my page that the database they were looking at was last changed to a specific date and time. For this, I have the following code:

 $lastUpdate = $pdo->prepare("SELECT lastUpdate FROM bng WHERE lastUpdate IN 
                            (SELECT max(lastUpdate) FROM bng)");
$lastUpdate->execute();

As I mentioned in the title, I want to repeat the result on my page, I tried:

<?php
echo '<p>Database Last Modified  on '.date("l, d F Y - h:i:s A",strtotime($lastUpdate)).'</p>';
?>

but it gives me wrong data and error message

Warning: strtotime () expects parameter 1 to be a string,

Hope this question makes sense. Thanks in advance.

+4
source share
2 answers

$lastUpdateis a PDO statement, not a value from a database. You need to call fetch()and then extract the value from the string.

$lastUpdate = $pdo->prepare("SELECT max(lastUpdate) AS lastUpdate FROM bng");
$lastUpdate->execute();
$row = $lastUpdate->fetch(PDO::FETCH_ASSOC);
echo '<p>Database Last Modified  on '.date("l, d F Y - h:i:s A",strtotime($row['lastUpdate'])).'</p>';
+1
source

$lastUpdate

echo '<p>Database Last Modified  on '.date("l, d F Y - h:i:s A",strtotime("1-1-2017")).'</p>';

Fetch Query

0

Source: https://habr.com/ru/post/1674716/


All Articles