I am using PHP and MySQL, and want to calculate date time difference between two datetimes. I have a message table, in that table createdate
is one field. I want to find out day and time difference from current date in the format 1 day 2 hours ago
. What is the best way to go about this?
Use PHP's built in date functions:
<?php
$start_time = "Y-m-d H:i:s"; // fill this in with actual time in this format
$end_time = "Y-m-d H:i:s"; // fill this in with actual time in this format
// both of the above formats are the same as what MySQL stores its
// DATETIMEs in
$start = new DateTime($start_time);
$interval = $start->diff(new DateTime($end_time));
echo $interval->format("d \d\a\y\s h \h\o\u\r\s");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With