Thread: TimeElapsed
View Single Post
Old 05-27-2009, 07:59 PM   #1 (permalink)
Sirupsen
The Contributor
 
Sirupsen's Avatar
 
Join Date: May 2009
Posts: 53
Thanks: 2
Sirupsen is on a distinguished road
Default TimeElapsed

Hello! I'm trying to make a scrupt which calculates estimated time since post, like on twitter. I kinda just want it to show minutes, untill its hours, show hours untill its days ago - and when it's at days, show the full date. But would I do this?

But yeah, I'm really really unsure how to do this? Anyone can help me a bit? Here's the code I have so far, if someone know a site or script for this I'd love a look on it..:

PHP Code:
    function calcElapsedTime($time){

        
$diff time()-$time;
        
$yearsDiff floor($diff/60/60/24/365);
        
$diff -= $yearsDiff*60*60*24*365;
        
$monthsDiff floor($diff/60/60/24/30);
        
$diff -= $monthsDiff*60*60*24*30;
        
$weeksDiff floor($diff/60/60/24/7);
        
$diff -= $weeksDiff*60*60*24*7;
        
$daysDiff floor($diff/60/60/24);
        
$diff -= $daysDiff*60*60*24;
        
$hrsDiff floor($diff/60/60);
        
$diff -= $hrsDiff*60*60;
        
$minsDiff floor($diff/60);
        
$diff -= $minsDiff*60;
        
$secsDiff $diff;
        
        
$ago 'ago';
        
$years $yearsDiff.' year'.(($yearsDiff <> 1) ? "s" "");
        
$months $monthsDiff.' month'.(($monthsDiff <> 1) ? "s" "");
        
$weeks $weeksDiff.' week'.(($weeksDiff <> 1) ? "s" "");
        
$days $daysDiff.' day'.(($daysDiff <> 1) ? "s" "");
        
$hours $hrsDiff.' hour'.(($hrsDiff <> 1) ? "s" "");
        
$mins $minsDiff.' minute'.(($minsDiff <> 1) ? "s" "");
        
$secs $secsDiff.' second'.(($secsDiff <> 1) ? "s" "");
        
        if(
$years 1) {
            
$years "";
        }    
        
        if(
$months 1) {
            
$months "";
        }        
        
        if(
$weeks 1) {
            
$weeks "";
        }        

        if(
$days 1) {
            
$days "";
        }        
        
        if(
$hours 1) {
            
$hours "";
        }
        
        if(
$mins 1) {
            
$mins "";
        }
        
        if(
$secs 1) {
            
$secs "";
        }
        
        return (
'
        
            '
.$years.
            '
.$months.'
            '
.$weeks.'
            '
.$days.
            '
.$hours.
            '
.$mins.
            '
.$secs.'
            '
.$ago.'
        
        '
);


Last edited by Sirupsen : 05-27-2009 at 08:37 PM.
Send a message via AIM to Sirupsen Send a message via MSN to Sirupsen Send a message via Yahoo to Sirupsen Send a message via Skype™ to Sirupsen
Sirupsen is offline  
Reply With Quote