Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing unix timestamps in PHP [closed]

Tags:

html

php

mysql

In PHP I have:

$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
echo floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

How do I get the difference in seconds? I've tried the following:

$diff = abs(strtotime(date('m/d/Y h:i:s')) - strtotime($latest));
like image 821
user1841964 Avatar asked Oct 19 '25 11:10

user1841964


1 Answers

Use DateTime instead, it will make your code much cleaner.

$latest = new DateTime($latest);
$now = new DateTime();

$diff = $latest->diff($now);
echo $diff->format('%y years %m months %d days');
like image 73
xdazz Avatar answered Oct 21 '25 01:10

xdazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!