5

Im looking for a simple way to calculate the time since TIMESTAMP entry on a mysql database. I would like to use php.

The timestamp is a unix format - 2013-01-15 12:46:11

If its currently 2013-01-15 19:46:11, what would be the most concise coding to calculate the difference the difference between the two?

Thanks for any help! Jason

John Woo
  • 258,903
  • 69
  • 498
  • 492
user1789437
  • 490
  • 1
  • 7
  • 22

4 Answers4

3
 echo ((strtotime("2013-01-15 19:46:11")-strtotime("2013-01-15 12:46:11"))/3600); 
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
3

You want DateInterval's format method: $date->diff

$date = new \DateTime();
$date->setTimestamp($timestamp);
$interval = $date->diff(new \DateTime('now'));
echo $interval->format('%y years, %m months, %d days, %h hours and %i minutes ago');

change the now to your date.

PHP Time Since Function?

Community
  • 1
  • 1
Adam Ramadhan
  • 22,712
  • 28
  • 84
  • 124
2

Try something like the following:

<?php 
    echo(time()-strtotime("2013-01-15 19:46:11"));
?>
Brian Hannay
  • 522
  • 4
  • 19
0

Take a look at some of below useful links.

https://gist.github.com/822283

how to calculate exact hours from two timestamp in php?

http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/

i recommend first link (GITHUB)

Community
  • 1
  • 1
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90