Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Web Programming  Home Web Programming PHP Minutes Ago
rss

Minutes Ago

Author: Will More by this author


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.



Author's URL: www.avengex.com

Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Minutes Ago"