Subtract mysql and php time

I want to find the time difference between mysql timestamp and time in php.

$timesince = strtotime("now") - $row['DateTime']; 

This is my attempt to do this, but it does not work.

In addition, ideally, I would like the result to be in a good format: 34 minutes ago, 2 hours 5 minutes ago 4, days 2 minutes ago, etc.

Really, really appreciate any help with this.

+4
source share
2 answers

strtotime("now") returns the time in seconds since Unix, i.e. integer. The mysql timestamp field must be YYYY-mm-dd HH: mm: ss. First you need them to be of the same format. I would recommend selecting the mysql timestamp field as UNIX_TIMESTAMP(DateTime) so that it returns an integer, and then you can do your math to get $timesince in seconds.

Then, as @Derek Adair said, check out the PHP Date Object

+6
source

Check out the PHP Date Object tutorial

just using date() returns the current date.

Try something like ...

 $curDate = date(); $mysqlTimestamp = $row['timestamp']; $dif = strtotime($curdate) - strtotime($mysqlTimestamp); 
+2
source

All Articles