Posted By

benoit on 12/15/09


Tagged

php date helper tool


Versions (?)

Temps relatif


 / Published in: PHP
 

Ceci est un fork du snippet que vous pourrez trouver ici : http://snipplr.com/view/4912/relative-time/

J'ai simplement traduit la fonction, et ajouté la possibilité de définir le langage à utiliser.

  1. if ( ! function_exists('plural'))
  2. {
  3. function plural($num = NULL)
  4. {
  5. if ($num != 1) return "s";
  6. }
  7. }
  8.  
  9.  
  10. if ( ! function_exists('relative_time'))
  11. {
  12. function relative_time($date = NULL, $lang = 'en')
  13. {
  14. $diff = time() - strtotime($date);
  15. if ($lang == 'en')
  16. {
  17. if ($diff<60)
  18. return $diff . " second" . plural($diff) . " ago";
  19. $diff = round($diff/60);
  20. if ($diff<60)
  21. return $diff . " minute" . plural($diff) . " ago";
  22. $diff = round($diff/60);
  23. if ($diff<24)
  24. return "about " . $diff . " hour" . plural($diff) . " ago";
  25. $diff = round($diff/24);
  26. if ($diff<7)
  27. return $diff . " day" . plural($diff) . " ago";
  28. $diff = round($diff/7);
  29. if ($diff<4)
  30. return $diff . " week" . plural($diff) . " ago";
  31. return "on " . date("F j, Y", strtotime($date));
  32. }
  33. elseif ($lang == 'fr')
  34. {
  35. if ($diff<60)
  36. return "il y a ". $diff . " seconde" . plural($diff);
  37. $diff = round($diff/60);
  38. if ($diff<60)
  39. return "il y a ". $diff . " minute" . plural($diff);
  40. $diff = round($diff/60);
  41. if ($diff<24)
  42. return "il y a environ ". $diff . " heure" . plural($diff);
  43. $diff = round($diff/24);
  44. if ($diff<7)
  45. return "il y a ". $diff . " jour" . plural($diff);
  46. $diff = round($diff/7);
  47. if ($diff<4)
  48. return "il y a ". $diff . " semaine" . plural($diff);
  49. return "le " . date("F j, Y", strtotime($date));
  50. }
  51. // elseif autre language...
  52. }
  53. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: benoit on December 15, 2009

Il y aurait quelques trucs à changer mais je ne sais pas comment éditer mon propre snippet!

if ( ! functionexists('relativetime')) { function relative_time($date = NULL, $lang = 'en') { $diff = time() - strtotime($date); if ($lang == 'en') { if ($diff

You need to login to post a comment.