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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.