Jump to content

time_since() HELP ON WEEKS!


iantearle

Recommended Posts

Hey everyone, been working on this time_since() for a while, basically want it to have the ability to put phrases instead of just time in days, minutes etc, for example if it was less than 30 seconds ago, i would want it to say, "saved a moment ago"

 

here is what i have so far, works great up until weeks! then it just buggers up and doesnt show it weeks, only days!

 

HELP!

 

/*
------------------------------------------------------------
Time Ago Stamp
============================================================
*/
function time_since($mbtime){ 
    /* Current unix time  */
        $now = time(); 

    $diff = $now - $mbtime;

/* Convert PHP time/date to legiable data, to do the math */ 
    $today = date('dmy', $now) === date('dmy', $mbtime) ? true : false; 
    
if($today && $diff < 10){ // if less than 10 seconds ago
	$num = ceil($diff / 60); 
	return 'a moment ago'; 
    }
elseif($today && $diff < 60){ // if less than a minute ago
        $num = ceil($diff / 1); 
        return $num . ' second' . ($num > 1 ? 's' : '') . ' ago';
}
elseif($today && $diff < 60 * 1){ // if less than 1 minute ago
        $num = ceil($diff / 60); 
        return 'about a minute ago';
}
elseif($today && $diff < 60 * 60){ // if less than an hour ago
        $num = ceil($diff / 60); 
        return $num . ' minute' . ($num > 1 ? 's' : '') . ' ago';
}
elseif($today && $diff < 60 * 60 * 2){ 
        $num = floor($diff / 60 / 60); // if hours ago
        return 'a couple of hours ago'; 
    }
elseif($today && $diff < 60 * 60 * 24){ 
        $num = floor($diff / 60 / 60); // if hours ago
        return $num . ' hour' . ($num > 1 ? 's' : '') . ' ago'; 
    }
elseif($today){ 
        $num = floor($diff / 60 / 60 / 24 / 7); // if hours ago
        return $num . ' day' . ($num > 1 ? 's' : '') . ' ago'; 
    }

else
	//($today && $diff < 60 * 60 * 24 * 7)
{ 
	$thisyear = date('y', $now) === date('y', $mbtime) ? true : false; 
        $daydiff = date('z', $now) - date('z', $mbtime); 
        if($daydiff === 1 && $thisyear){ 
            return 'Yesterday'; 
        }
        $num = ceil($diff / 60 / 60 / 24 / 7); // if Days ago
        return $num . ' week' . ($num > 1 ? 's' : '') . ' ago';


    }
    return $mbtime; 
}

Link to comment
https://forums.phpfreaks.com/topic/102104-time_since-help-on-weeks/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.