Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time subtract mysql and php

Tags:

php

mysql

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

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

This is my attempt at doing it but it doesn't seem to work.

Also, really ideally I'd like the output to be in a nice format like: 34 minutes ago,2 hours 5 minutes ago 4,days 2 minutes ago etc.

Really, really appreciate any help on this.

like image 951
Sam Avatar asked Nov 28 '10 17:11

Sam


1 Answers

strtotime("now") returns the time as seconds since the Unix epoch, i.e. an integer. A mysql "timestamp" field is going to be YYYY-mm-dd HH:mm:ss. You first need to get those to be the same format. I'd recommend selecting the mysql timestamp field as UNIX_TIMESTAMP(DateTime) so it returns an integer, and then you'll be able to do your math to get $timesince in seconds.

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

like image 137
Vic Avatar answered Sep 23 '22 07:09

Vic