iantearle.com

Time Since MBTime

Since writing my last post on the time_since() function, I became frustrated and tore apart the original script, to write my own time_since() function. This function is highly adaptable for those who want to have multiple phrases for the amount of time that has passed.

For example, if you would like to say "just a moment ago" for less than 10 seconds ago, or "about a minute ago" for time within a minute ago you can. You can even say your post was left "yesterday". I have not finished developing the script, but here is what i have done so far.

/*
 —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  —  — 
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;
   
    /* Here is the fun part, how many phrases can you think of?
        This may help: some Math.
        the $diff is the amount of time that has passed. So to get the amount of
        seconds, minutes, hours passed you need to divide by the figure you wish.
        So say you want seconds:
            $diff / 1
        minutes:
            $diff / 60
        hours:
            $diff / 60 * 60
        days:
            $diff / 60 * 60 * 24
        The same equations can be made for defining if the time should be less
        than an amount, so if you want to say something for less than 10 seconds
        ago:
            $diff < 10
        less than an hour:
            $diff < 60 * 60
    */
   
    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){
        $num = floor($diff / 60 / 60); // if hours ago
        return $num . ’ hour’ . ($num > 1 ? ‘s’ : ”) . ’ ago’;
    }
    else{
        $thisyear = date(‘y’, $now) === date(‘y’, $mbtime) ? true : false;
        $daydiff = date(‘z’, $now) - date(‘z’, $mbtime);
        if($daydiff === 1 && $thisyear){
            return ‘Yesterday’;
        }
    }
    return $mbtime;
}

Posted On: Friday, April 18th, 2008 @ 5:23 pm by Ian Tearle


0 Comments

There are no comments on this entry. You should add one below.

Leave a comment

Be kind

If you would like to discuss anything about this post, please be polite and add your comments below.


 
 

Additionally

No HTML is allowed. All links, and new lines will be automagically converted to HTML for you.
Notice to spammers, all links will be sanitized with rel="nofollow".


Contact Me

Get In Touch

If you would like to discuss anything about your next project please drop me a line.


 

Prices

Projects start at £99.00 per month
We normally book 2–3 months in advance.

Additionally

No matter how small your project is, or if the prices above scare you, please do get in touch, I shall always listen and will advise freely alternative ideas or solutions. If you are a charity, let's talk!


Find Me On