hi, can someone provide me with a script to know the difference between two dates ? i searched everywhere but the info was vague. the problem is like this: i have a table which stores the date of the last login by user. i want this script to show after exactly how many days is the user loggin in(since his last login). please help me ! thanks in advance
I typed this up a while ago. Should work for you if you can convert the dates to a timestamp. If you don't have the timestamp available, you can convert pretty much any date format to a timestamp with http://www.php.net/mktime
Both parameters are UNIX timestamps. The first param is obviously the current time. The second param is the original recorded time you're comparing it to... so the first param will be larger than the second. If the first param is smaller, it will prepend a hyphen (negative sign).
It will display from years all the way down to seconds. It will display an increment of time only if it is greater than zero, so it will return something like
Code:
3 weeks, 2 days, 6 hours, 10 minutes, 34 seconds
So if the difference is 3 seconds more than a week, it will return only
Code:
1 week, 3 seconds
meaning it will NOT display useless data like
Code:
1 week, 0 days, 0 hours, 0 minutes, 3 seconds
If you don't want it to display too meticulously (like all the way down to seconds or minutes), just comment out or delete the blocks you don't want displayed (between their respective "Start" and "End" comments).
PHP Code:
<?php
function CalcDiff($currTime, $origTime)
{
$showDiff = '';
// Set times
$currTime = intval($currTime);
$origTime = intval($origTime);
if ($currTime<$origTime) { $diff = $origTime-$currTime; }
else { $diff = $currTime-$origTime; }
// Start Years
$yrs = floor($diff/31556926); // 31556926 secs/yr
if ($yrs > 0)
{
$diff = $diff - ($yrs*31556926);
$showDiff .= "{$yrs} year";
$showDiff .= $yrs>1 ? 's' : '';
}
unset($yrs); // End Years