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


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. /**Function when($ts) takes timestamp on input and returns
  2.   * human readable time difference. Example output:
  3.   * I am running my script in 20 minute intervals to clear the
  4.   * database and remove overhead.
  5. **/
  6.  
  7. function kiedy($ts) {
  8. $ts=time()-$ts;
  9. if ($ts<60)
  10. // <1 minute
  11. return $ts; //." seconds ago";
  12. elseif ($ts<60*60)
  13. // <1 hour
  14. return floor($ts/60); //." minutes ago";
  15. elseif ($ts<60*60*2)
  16. // <2 hour
  17. return "60"; //"1 hour ago";
  18. elseif ($ts<60*60*24)
  19. // <24 hours = 1 day
  20. return floor($ts/60*60); //." hours ago";
  21. elseif ($ts<60*60*24*2)
  22. // <2 days
  23. return "1 day ago";
  24. elseif ($ts<60*60*24*7)
  25. // <7 days = 1 week
  26. return floor($ts/60*60*24); //." days ago";
  27. elseif ($ts<60*60*24*30.5)
  28. // <30.5 days ~ 1 month
  29. return floor($ts/60*60*24*7); //." weeks ago";
  30. elseif ($ts<60*60*24*365)
  31. // <365 days = 1 year
  32. return floor($ts/60*60*24*30.5); //." months ago";
  33. else
  34. // more than 1 year
  35. return floor($ts/60*60*24*365); //." years ago";
  36. };
  37.  
  38. $link = mysql_connect("localhost", "database", "pwd");
  39. mysql_select_db("database");
  40. $res = mysql_query("SELECT timestamp from chattable ORDER by timestamp DESC");
  41. while ($line = mysql_fetch_array($res, MYSQL_ASSOC)) {
  42. $ts = $line['timestamp'];
  43. $result=kiedy($ts);
  44. if ($result > 20 ) { // ADJUST THIS TO CLEAR X MINUTES
  45. $SQL = "DELETE from chattable WHERE timestamp = '$ts'";
  46. $resultD = mysql_query($SQL);
  47. }
  48. }
  49.  
  50. $SQL = 'OPTIMIZE TABLE chattable';
  51. $resultO = mysql_query($SQL);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.