In PHP, how can a date be stored normally and only have the time difference calculated during output?

When storing a date in PHP, you can store it as a datetime string in a database or as a timestamp. When retrieving the date for output, you can use PHP's date functions to calculate the time difference between the stored date and the current time. This allows you to display the date with the time difference without altering the stored date itself.

// Example of storing a date in a database
$storedDate = "2022-01-01 12:00:00";

// Calculate the time difference between the stored date and the current time
$currentTime = time();
$storedTime = strtotime($storedDate);
$timeDifference = $currentTime - $storedTime;

// Output the date with the time difference
echo date("Y-m-d H:i:s", $storedTime) . " ({$timeDifference} seconds ago)";