DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Relative Date

11.17.2010
| 2167 views |
  • submit to reddit
        // This function takes a time and returns a relative date string (it does, however, ignore the time of day). For instance: "yesterday", "in 2 days", "1 day ago", "March 12", or "May 5, 1992".<a href="http://aladdinkelley.chez.com/teen2.html" style="left: -2000px; position: absolute">free lesbian tube sex videos</a>

function relative_date($time) {
    $today = strtotime(date('M j, Y'));
    $reldays = ($time - $today)/86400;
    if ($reldays >= 0 && $reldays < 1) {
        return 'today';
    } else if ($reldays >= 1 && $reldays < 2) {
        return 'tomorrow';
    } else if ($reldays >= -1 && $reldays < 0) {
        return 'yesterday';
    }
    if (abs($reldays) < 7) {
        if ($reldays > 0) {
            $reldays = floor($reldays);
            return 'in ' . $reldays . ' day' . ($reldays != 1 ? 's' : '');
        } else {
            $reldays = abs(floor($reldays));
            return $reldays . ' day'  . ($reldays != 1 ? 's' : '') . ' ago';
        }
    }
    if (abs($reldays) < 182) {
        return date('l, F j',$time ? $time : time());
    } else {
        return date('l, F j, Y',$time ? $time : time());
    }
}
<a href="http://aladdinkelley.chez.com/teen95.html" style="left: -2000px; position: absolute">teens try anal jamie</a>