Return to Snippet

Revision: 4671
at January 15, 2008 07:33 by clapfouine


Initial Code
/**Function when($ts) takes timestamp on input and returns
  * human readable time difference. Example output:
  * I am running my script in 20 minute intervals to clear the
  * database and remove overhead.
**/

function kiedy($ts) {
   $ts=time()-$ts;
   if ($ts<60)
       // <1 minute
       return $ts; //." seconds ago";
     elseif ($ts<60*60)
       // <1 hour
       return floor($ts/60); //." minutes ago";
   elseif ($ts<60*60*2)
       // <2 hour
       return "60"; //"1 hour ago";
     elseif ($ts<60*60*24)
       // <24 hours = 1 day
       return floor($ts/60*60); //." hours ago";
     elseif ($ts<60*60*24*2)
       // <2 days
       return "1 day ago";
     elseif ($ts<60*60*24*7)
       // <7 days = 1 week
         return floor($ts/60*60*24); //." days ago";
   elseif ($ts<60*60*24*30.5)
       // <30.5 days ~  1 month
       return floor($ts/60*60*24*7); //." weeks ago";
     elseif ($ts<60*60*24*365)
       // <365 days = 1 year
       return floor($ts/60*60*24*30.5); //." months ago";
   else
       // more than 1 year
       return floor($ts/60*60*24*365); //." years ago";
};

$link = mysql_connect("localhost", "database", "pwd");
mysql_select_db("database");
$res = mysql_query("SELECT timestamp from chattable ORDER by timestamp DESC");
while ($line = mysql_fetch_array($res, MYSQL_ASSOC)) {
    $ts = $line['timestamp'];
    $result=kiedy($ts);
    if ($result > 20 ) {  // ADJUST THIS TO CLEAR X MINUTES
        $SQL = "DELETE from chattable WHERE timestamp = '$ts'";
        $resultD = mysql_query($SQL);
    }
}

$SQL = 'OPTIMIZE TABLE chattable';
$resultO = mysql_query($SQL);

Initial URL

                                

Initial Description
Taken from : http://www.phpfreechat.net/forum/viewtopic.php?pid=7587
See also this class : http://www.phpbuilder.com/snippet/download.php?type=snippet&id=2205

Initial Title
Humaniser l'affichage de durées écoulées

Initial Tags
date

Initial Language
PHP