skip to Main Content

I made a PHP website that use a SQL database for the information.
Now I want to show the date of the last update of that database.

My code works great, except, when I run the command in phpMyAdmin I get the (right) date:

UPDATE_TIME
2024-08-10 10:40:08

The website show the date of today:

update: 13-08-2024

$sql = "SELECT update_time FROM information_schema.tables WHERE TABLE_NAME = 'katten'";
$tableStatus = mysqli_query($link, $sql);

while ($array = mysqli_fetch_array($tableStatus))

{
    $updatetime = $array['UPDATE_TIME'];
}
echo date ('d-m-Y', $updatetime);

What I’m doing wrong?

2

Answers


  1. The issue you’re encountering stems from the fact that the UPDATE_TIME value retrieved from the database is a string (formatted as YYYY-MM-DD HH:MM:SS), not a Unix timestamp, which the date() function expects. To fix this, you need to convert the UPDATE_TIME string into a Unix timestamp before passing it to the date() function.

    Here’s how you can modify your code:

    $sql = "SELECT update_time FROM information_schema.tables WHERE TABLE_NAME = 'katten'";
    $tableStatus = mysqli_query($link, $sql);
    
    $array = mysqli_fetch_array($tableStatus);
    // Convert the update_time string to a Unix timestamp
    $updatetime = strtotime($array['update_time']);
    
    
    // Format and display the date
    echo date('d-m-Y', $updatetime);
    
    Login or Signup to reply.
  2. You need to feed a timestamp value the PHP date() function as in:

    echo date ('d-m-Y', strtotime($updatetime));
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search