-1

in my sql table I save the date and time of a certain action, how can I print it via php on the contrary, or how long ago did it take place?

for example

|date                |
|05-28-2020 17:12:05 |

and at 17:30:05 it will be written on my php page "18 minutes ago"

RST
  • 3,899
  • 2
  • 20
  • 33
  • Does this answer your question? [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – Kulshreshth K May 28 '20 at 15:37

1 Answers1

0
  1. Load the date from the database using a mysqli_query and store the result in $original_date.
  2. Create a date object from the string in the database.
$old_date = new DateTime($original_date);
  1. Get the current date in the same format and store it in another variable.
$current_date = new DateTime(date("m-d-Y H:m:s"));
  1. Get the difference between the two dates.
$difference = $old_date->diff($current_date);
  1. Convert the difference to minutes and display message.
echo($difference->m . "minutes ago");

Here is the full code for you to use:

# Get database value and store in $original_date

$old_date = new DateTime($original_date);
$current_date = new DateTime(date("m-d-Y H:m:s"));
$difference = $old_date->diff($current_date);

echo($difference->m . "minutes ago");
Run_Script
  • 2,487
  • 2
  • 15
  • 30