Hey fellow programmers,
just wondering if anyone has a code snippet or has found a tutorial on how to build web 2.0 style dates ~ i.e. "last seen online 2 hours ago", "last seen online yesterday", "last seen online 2 months ago" etc
Cheers
| SitePoint Sponsor |
Hey fellow programmers,
just wondering if anyone has a code snippet or has found a tutorial on how to build web 2.0 style dates ~ i.e. "last seen online 2 hours ago", "last seen online yesterday", "last seen online 2 months ago" etc
Cheers
_________________________/Az.________________


Most likely just date manipulation based on the users last logged in time, here is a little script I wrote that gives you the difference in days. To use hours have a look at the date functions in the php manual......
CheersPHP Code:function getdays($day1,$day2)
{
$diff = round((strtotime($day2)-strtotime($day1))/(24*60*60),0);
if(eregi('-', $diff)) {
$dr = str_replace("-", "", $diff);
$suffix = ($dr > 1) ? ' days ' : ' day ';
$out = 'last seen '. $dr . $suffix .' ago';
}
return $out;
}
$begin = date("Y-m-d"); // we set today as an example
$end = "2006-09-19";
echo getdays($begin,$end);
Spike
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!
I am amazed you called it Web 2.0 style date?just wondering if anyone has a code snippet or has found a tutorial on how to build web 2.0 style dates ~ i.e. "last seen online 2 hours ago"
cheers spikeZ, i'll work of your code on that one.
lol - why are you suprised? its so web 2.0 it hurts !
_________________________/Az.________________
He only said that because he has a Web 1.0 hairdo.![]()
Upgrading to Mysql 5? Auto-increment fields now strict
use NULL
Or zero or leave the field name out completely.
ha ha ha - web 2.0 is such a dirty word, but when a client specifically asks for a web 2.0 style site, what can you do but deliver?
But i feel dirty, real dirty.
_________________________/Az.________________
Well, you do accept Money 2.0 dont you?
You can't have your Cake 2.0 and it Eat It 1.0.
tehee... Still I cant help it, I have to ask you, how will your client know the difference between web 1.0 and web 2.0 dates?
I mean, do they auto-refresh with Ajax or what? Are the dates clickable - and form part of a tag-cloud?
I am really curious, and, well, I might be envious....
Upgrading to Mysql 5? Auto-increment fields now strict
use NULL
Or zero or leave the field name out completely.
lol - well the whole date thing started when I did the design, I put down things like "this comment was posted 3 days ago" and the client was like "oooh, I like how there aren't stupid dates there, but instead some friendly text, how web 2.0" etc
his one of those clients that know enough to hinder you, but not enough to realise how much work is involved :P
_________________________/Az.________________
LOL (yeah, really I did....)
I'm off now, but that made me larf. So like, the mysql date_format() function is web 2.0 ready...
keep well.
Upgrading to Mysql 5? Auto-increment fields now strict
use NULL
Or zero or leave the field name out completely.


Here's what I use:
modify to your heart's contentPHP Code://$time is a unix timestamp
function relative_date($time) {
$today = strtotime(date('M j, Y'));
$reldays = ($time - $today)/86400;
if ($reldays >= 0 && $reldays < 1) {
return 'Today';
} else if ($reldays >= 1 && $reldays < 2) {
return 'Tomorrow';
} else if ($reldays >= -1 && $reldays < 0) {
return 'Yesterday';
}
if (abs($reldays) < 7) {
if ($reldays > 0) {
$reldays = floor($reldays);
return 'In ' . $reldays . ' day' . ($reldays != 1 ? 's' : '');
} else {
$reldays = abs(floor($reldays));
return $reldays . ' day' . ($reldays != 1 ? 's' : '') . ' ago';
}
}
if (abs($reldays) < 21) {
return date('l, F j',$time ? $time : time());
} else {
return date('l, F j, Y',$time ? $time : time());
}
}
----------------
bob.kennedy
----------------




gawd you people are so last year
web 3.0 is the future
Bookmarks