casbboy
September 12, 2005, 12:59am
1
I am building a site where you can post certain media and articles. I want to have a note that says Posted however long ago, be it minutes, hours, or days.
How can I do this?
I can do a now() time stamp on the posts to work with.
Unless I should use something else.
Thanks
Ryan
I’d really like to know how to do this too, it’d be useful knowledge for a project I’m working on.
When the record is inserted:
INSERT mytable (posttime) VALUES (NOW());
When fetched:
SELECT posttime, NOW() AS currtime FROM mytable;
Use data/time function and do the math for the difference.
sweatje
September 12, 2005, 2:44am
4
Perhaps you are thinking something on the order of:
function seconds_ago_to_desc($ago) {
if ($ago >= 86400) {
return floor($ago/86400).' days ago';
} elseif ($ago >= 3600) {
return floor($ago/3600).' hours ago';
} elseif ($ago >= 60) {
return floor($ago/60).' minutes ago';
} else {
return $ago.' seconds ago';
}
}
casbboy
September 12, 2005, 4:21am
5
How do I get my $ago variable?
I have a column titled ‘date2’ that has the datetime format…
Thanks
Ryan
casbboy
September 12, 2005, 4:46am
6
This is what I went with:
$todaydate = date("Y-m-d H:i:s");
$ago = strtotime($todaydate) - strtotime($daily['date2']);
if ($ago >= 86400) {
$diff = floor($ago/86400).' days ago';
} elseif ($ago >= 3600) {
$diff = floor($ago/3600).' hours ago';
} elseif ($ago >= 60) {
$diff = floor($ago/60).' minutes ago';
} else {
$diff = $ago.' seconds ago';
}
//then echo $diff later
Is there an easier way?
Thanks
Ryan
Looks perfect to me, you should wrap the date_to_second functionality in a function (better in a class), as it is likely to be reused.