Hi, Guest ~ Login or Register

Minutes Ago

Minutes Ago

Listed In PHP and MySQL » General Development — Viewing Full Tutorial
Introduction
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;and 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.. if you have problems just post in ye' olde forums!

Working Beta

  1. The Forums
    These are mostly functional. If you see any weird bugs, post a thread about it and an administrator will do something.
  2. Tutorial Writing
    You can now submit tutorials to the brand new management system.
  3. Tutorials Home
    View tutorials by categories and search for them here.
  4. Shoutbox
    See below. Registered users only!

Register

Newest User

Say hi to Lirette25! Lirette25 joined on Monday, 14th July.

Sponsor

Check out Next day fake id

Shoutbox