Minutes Ago

Minutes AgoIntroduction

This tutorial is very useful for displaying how long ago something happened. Ideal for shoutboxes, forum posts etc.

What do you need?

First of all you need a Unix timestamp, good knowledge of PHP and a logical mind. Don't have any of those? You're probably sub-geek. If you don't have a unix timestamp, just use time() instead.

Ok, first of all we set up our time spans:

$second = 1;
$minute = $second*60;
$hour = $minute*60;
$day = $hour*24;
$week = $day*7;

As you can see it's quite straight forward..

Now we grab time:

$time = time();

and then get an offset:

$offset = 18734611;

nd lastly we find out the difference, the most important variable in this tutorial:

$difference = $time-$offset;

Now we've got the important variables we just start the fun loopy mathematically challenged bit, here it is in one big chunk:

$wcount = 0;
for($wcount = 0; $difference>$week; $wcount++) {
$difference = $difference - $week;
}
$dcount = 0;
for($dcount = 0; $difference>$day; $dcount++) {
$difference = $difference - $day;
}
$hcount = 0;
for($hcount = 0; $difference>$hour; $hcount++) {
$difference = $difference - $hour;
}
$mcount = 0;
for($mcount = 0; $difference>$minute;
$mcount++) {
$difference = $difference - $minute;
}

Might look slightly nasty and confusing, but all it does is set a variable for the amount of unit there is in the difference (e.g. day) and adds it up for each one. If there isn't enough of the difference to take away and keep it a positive value it then cycles on to the next smallest unit, e.g. going from weeks to days to hours.

How do I show the amount of time then?

Simple, just use this:

Weeks ago: <?=$wcount?>
Days ago: <?=$dcount?>
Hours ago: <?=$hcount?>
Minutes ago: <?=$mcount?>
Seconds ago: <?=$difference?>

This is shown at this webpage in the development scrapheap.

Thanks.


Related Articles

  • Minutes Ago
    Grab minutes ago with unix time.
  • How I Made Over $150.00 In Profit With About 20 minutes Of Work!
    Reselling domain names can be a very profitable experience. I stumbled upon this little known money maker a few years ago when deciding to sell off my first domain. I knew that I would not have the time to develop it, so I listed it at eBay for a starting bid of $9.00.
  • Relative Dates
    In this tutorial I am going to teach you how to create your own relative date function.
  • How to Tell if a Host is Dodgy
    There are literally thousands of webhosts today. Every single one of them is after your business. But how should you choose which of these companies is right for you? How should you be able to determine a hosts integrity and professionalism without risking being ripped off?
  • Stop Analyzing, Start Promoting Your Web Site!
    As web promotion grows more and more popular everyday, so does over analyzing your stats, rankings, page rank, popularity rank, etc. We are all caught up in an analyzing funnel that seems to have no end in sight...
  • Don't Let Them Steal Your Affiliate URL!
    Cloaking is the magic word that will help you both to keep your commissions and make your affiliate URL look more appealing. It takes 10 minutes of work with the guidance in our tutorial.
  • Five Solid Reasons To Publish Your Ezine In A Blog!
    Five Solid Reasons To Publish Your Ezine In a Blog! Placing your ezine in a Blog or a RSS Feed only takes minutes to do. Its easy, fast and free. Theres no reason why you shouldnt take advantage of this new technology...
  • When Is 'Spying On Your Competition' A Complete Waste Of Time?
    Your brain screams Gimme Fast, Gimme Easy, Do it for me automatically! So when you see the headline that reads... Imagine Spying On Your Competition To Build A Massive Targeted Keyword List Of 3000 Or More Keywords On Auto-Pilot... In 10 Minutes Or Less And Skyrocket Your Adsense P...
  • Website Behaviour
    A good web site behaves like a top-class butler or a concierge in a hotel. It looks after you, it intelligently anticipates your needs, and it goes out of its way not to make you feel bad. When everythings okay, it lets you get on with your business...
  • Keyword and Meta Description Tags
    Keyword and description meta tags optimization tutorial for better search engine rankings. Have you ever wondered

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.