Posted x Time Ago Script

Acer Aspire one d250 aod250-1325 netbook
July 2, 2010 at 17:47:43
Specs: Windows 7
Hi Guys, So basically my new website http://mafanikio.co.cc/ has a latest news feature. The thing is is I can get it to display the latest news from my database, and display all relevant deails within, but I can't for the life of me figure out how to make a script that will show me how long ago it was posted.

I need something that would calculate the time ago in seconds and then I could echo the relevant time e.g. x Seconds Ago, x Minutes Ago, x Hours Ago, x Days Ago.

Just to clear it up i'll show you the PHP iv'e got so far;

 $result = mysql_query("SELECT * FROM `news`");
    $num_rows = mysql_num_rows($result);
    $itemid = $num_rows;
    $itemid = $num_rows;
    $sql = "SELECT * FROM `news` WHERE `id`='".$itemid."'";
    $res = mysql_query($sql) or die("Error");
    $row = mysql_fetch_assoc($res);
    $time = $row['date'];

And on my page I have;

<div class="sidebarheader">latest news</div>
		<div class="content">
		<strong><? echo $row['title']; ?></strong><br />
		<? echo $row['small-article']; ?>
		<br /><br />
		<span class="right">
		<? echo $row['author']; ?><br />
		<i>Posted <?  echo $ago;   ?></i><br />
		</span>
		<br/>
		</div>

Any help would be MEGA appreciated and thanks in advance.

V/r
Mafanikio


See More: Posted x Time Ago Script

Report •


#1
July 2, 2010 at 20:14:56
Help, I really need Help :))

Report •

#2
July 2, 2010 at 21:52:44
Not really tested so I don't know how accurate it is, but something like below *may* work; I don't know the data types your table has, so I created a simple one to try things out.

<?php

   $link = mysql_connect("", "", "") or die(mysql_error());

   mysql_query("create database if not exists db_compnet;") or die(mysql_error());
   mysql_select_db("db_compnet", $link) or die(mysql_error());

   mysql_query("drop table if exists tbl_test;") or die(mysql_error());

   mysql_query("create table if not exists tbl_test(
      ID int(5) auto_increment,            
      tstamp bigint(6) not null default 0,
      primary key(ID)
   )type=myisam;") or die(mysql_error());

   mysql_query("insert into tbl_test values('null', '" . 
                mktime(12, 0, 0, 1, 1, 2010) . "');") or die(mysql_error());

   mysql_query("insert into tbl_test values('null', '" . 
                mktime(23, 59, 59, 12, 24, 2000) . "');") or die(mysql_error());

   mysql_query("insert into tbl_test values('null', '" . 
                (time() + 1024) . "');") or die(mysql_error());

   mysql_query("insert into tbl_test values('null', '" . 
                (time() + 4096) . "');") or die(mysql_error());

   $res = mysql_query("select *, (to_days(now()) - to_days(from_unixtime(tstamp))) as dtime" .
                      " from tbl_test order by dtime asc;") or die(mysql_error());

   $ctime = time();

   if(mysql_num_rows($res) > 0) {
      while($row = mysql_fetch_assoc($res)) { 
         echo date('F d, Y g:i A', $row[tstamp]) . " occured " .
              (($row[tstamp] - $ctime) < 0 ?  
               $row[dtime] : convertTS($row[tstamp] - $ctime)) . 
              " days ago<BR />";         
      }
   }

   mysql_close($link); 

function convertTS($nsecs) {

   return (floor($nsecs / 3600) . " hours " .
           floor(($nsecs % 3600) / 60) . " minutes " .
           ceil(($nsecs % 3600) % 60) . " seconds");
}
?>

Hopefully it (or some variation) will work for you.
HTH


Report •

#3
July 3, 2010 at 16:32:13
Sorry I need something that will tell me the time ago in seconds and then I could echo the relevant time e.g. x Seconds Ago, x Minutes Ago, x Hours Ago, x Days Ago.

Report •

Related Solutions

#4
July 3, 2010 at 20:26:44
Can't win 'em all. :D

I don't know how $row['date'] is stored, but maybe something like below will help...

<?php

    $ctime = time(); 

    $cdate = date('M d Y g:i:s A', $ctime);
    $pdate = date('M d Y g:i:s A', $ctime - 1635);
    $nsecs = strtotime($cdate) - strtotime($pdate);

    echo "starting time in secs: $ctime<br />" .
         "date (current): $cdate<br />" .
         "date (27min 15 secs ago): $pdate<br />" .
         "diff in secs: $nsecs";    
?>

HTH


Report •

#5
July 4, 2010 at 10:41:26
I dont think I explained it correctly. When I post a news article I store the date in three separate fields, $row['day'], $row['hour'] and $row['year']. Now i need some thing that if the script was posted, lets say 27 minutes ago, it would echo 27 minutes ago.

Now I cant seem to figure out how long ago it wass posted e.g 1 hour ago, 27 minutes ago, 2 days ago and then echo the time.

Kind of link a twitter status.


Report •


Ask Question