0

I spent some time doing this quick little function (I didn't use the default one because I wanted a bit more customization later on). I made a post that has $checkTime = '0';, and when run through this function it comes back as 49 years ago.

Why is it returning that when January 1970 was only 45 years ago? Are the extra 4 years coming from time differences and leap years?

The other times seem to work correct (recent ones), but the ones I set to 0 say that and I'm just curious where the bug is, or what I might be overlooking.

function relativeTime($string) {
    $currentTime = time();
    $checkTime = $string;

    $timeDifference = $currentTime - $checkTime;

    if($timeDifference > '0') {
        $timeSeconds = round(($timeDifference / 60) * 60);
        $timeMinutes = round($timeSeconds / 60);
        $timeHours = round($timeMinutes / 60);
        $timeDays = round($timeHours / 24);
        $timeWeeks = round($timeDays / 7);
        $timeMonths = round($timeWeeks / 4);
        $timeYears = round($timeMonths / 12);

        if($timeSeconds < '2') {
            return ''.$timeSeconds.' second ago';
        } elseif($timeSeconds < '60') {
            return ''.$timeSeconds.' seconds ago';
        } elseif($timeMinutes < '2') {
            return ''.$timeMinutes.' minute ago';
        } elseif($timeMinutes < '60') {
            return ''.$timeMinutes.' minutes ago';
        } elseif($timeHours < '2') {
            return ''.$timeHours.' hour ago';
        } elseif($timeHours < '24') {
            return ''.$timeHours.' hours ago';
        } elseif($timeDays < '2') {
            return ''.$timeDays.' day ago';
        } elseif($timeDays < '7') {
            return ''.$timeDays.' days ago';
        } elseif($timeWeeks < '2') {
            return ''.$timeWeeks.' week ago';
        } elseif($timeWeeks < '4') {
            return ''.$timeWeeks.' weeks ago';
        } elseif($timeMonths < '2') {
            return ''.$timeMonths.' month ago';
        } elseif($timeMonths < '12') {
            return ''.$timeMonths.' months ago';
        } elseif($timeYears < '2') {
            return ''.$timeYears.' year ago';
        } elseif($timeYears > '1') {
            return ''.$timeYears.' years ago';
        } else {
            return $timeSeconds;
        }

    } else {
        return 'The Future';
    }
}
Drennon
  • 15
  • 4
  • I've tried removing the rounding just in case that was doing it, and still the same problem (except now it's 48.98... years). – Drennon Jan 24 '15 at 15:33
  • 6
    Yes, you created quite a few leap years by making each month just 4*7 days. It's on average more like 30.4 days per month, not just 28. – mario Jan 24 '15 at 15:33
  • Are you writing this from the future? – Shomz Jan 24 '15 at 15:35
  • I can't believe that was the problem. Thank you @mario ! I've changed the `4` to `4.348` on the division for months, as that is the average number of weeks in each month. – Drennon Jan 24 '15 at 15:42
  • see examples from: http://php.net/manual/en/function.date-diff.php – dragonjet Jan 24 '15 at 16:04

1 Answers1

1

Because your calculations are messed up. See one example and check all your formulas

<?
//same numbers, different formula
$checkTime=0;
echo (time()-$checkTime)/31536000;    //45.094949422882 Years
?>

31536000 is the number of seconds in 1 year.

Even using that you will have to take care about leap years. We cant divide a timestamp by minutes then by hours then by days and so on. If you need accurate results the input has to be accurate as well.

Remember the famous Bi-Weekly and Twice Monthly payouts used commonly in USA? From a distance they both appear to mean the same thing but they don't. So dividing like your code is doing looses all that accuracy and when that difference is multiplied to 45 years it becomes substantial.

Fiddle

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • If you edit your answer to include that I could just change weeks per month from 4 to 4.348 (read the comments on my question), then I'll mark it as answered for future reference. It's probably easier doing it that way than yours, because then you don't have to deal in seconds. Thank you. :) – Drennon Jan 24 '15 at 15:59
  • Nope that is inaccurate either. It just solves your current problem at hand, it doesnt make that code accurate. Months are not the only things we need to be accurate. This page offers a good insight into this problem http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago?rq=1 – Hanky Panky Jan 24 '15 at 16:03
  • Testing it now and will mark as solved if it works. :) – Drennon Jan 24 '15 at 16:08