How to implement facebook like timestamp difference function?

How to implement facebook like timestamp difference function?



function time_parser($time)
    {

        if(!is_numeric($time))
        {
            $time = strtotime($time);
            if(!is_numeric($time))
            {
                return "";
            }
        }
        $difference = time() - $time;
        $periods = array("sec","min","hour","day","week","month","year");
        $lengths = array("60","60","24","7","4.35","12","10");

        if($difference > 0)
        {
            $ending = "ago";
        }
        else
        {
            $difference = -$difference;
            $ending = "to go";
        }
        for($j=0; $difference>=$lengths[$j] && $j<7;$j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);
        if($difference!=1)
        {
            $periods[$j].="s";
        }
                    $text = "$difference $periods[$j] $ending";
        return $text;
    }

Comments

Popular posts from this blog

How to build unlimited levels menu through PHP and MySQL

How to build a map of the frequency with which characters occur in a file in OCaml?

How to create timed pop-up windows using MVVM?