function.agestring.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Smarty function to turn an age in seconds into a human-readable string
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Smarty
  9. *
  10. * @param array $params parameter array
  11. * @param Smarty_Internal_Template $template smarty template
  12. * @return string human readable string
  13. */
  14. function smarty_function_agestring($params, Smarty_Internal_Template $template)
  15. {
  16. if (empty($params['age'])) {
  17. trigger_error("agestring: missing 'age' parameter");
  18. return;
  19. }
  20. $age = $params['age'];
  21. $resource = $template->getTemplateVars('resource');
  22. $output = null;
  23. if ($age > 60*60*24*365*2) {
  24. $years = (int)($age/60/60/24/365);
  25. if ($resource)
  26. $output = sprintf($resource->ngettext('%1$d year ago', '%1$d years ago', $years), $years);
  27. else
  28. $output = sprintf($years == 1 ? '%1$d year ago' : '%1$d years ago', $years);
  29. } else if ($age > 60*60*24*(365/12)*2) {
  30. $months = (int)($age/60/60/24/(365/12));
  31. if ($resource)
  32. $output = sprintf($resource->ngettext('%1$d month ago', '%1$d months ago', $months), $months);
  33. else
  34. $output = sprintf($months == 1 ? '%1$d month ago' : '%1$d months ago', $months);
  35. } else if ($age > 60*60*24*7*2) {
  36. $weeks = (int)($age/60/60/24/7);
  37. if ($resource)
  38. $output = sprintf($resource->ngettext('%1$d week ago', '%1$d weeks ago', $weeks), $weeks);
  39. else
  40. $output = sprintf($weeks == 1 ? '%1$d week ago' : '%1$d weeks ago', $weeks);
  41. } else if ($age > 60*60*24*2) {
  42. $days = (int)($age/60/60/24);
  43. if ($resource)
  44. $output = sprintf($resource->ngettext('%1$d day ago', '%1$d days ago', $days), $days);
  45. else
  46. $output = sprintf($days == 1 ? '%1$d day ago' : '%1$d days ago', $days);
  47. } else if ($age > 60*60*2) {
  48. $hours = (int)($age/60/60);
  49. if ($resource)
  50. $output = sprintf($resource->ngettext('%1$d hour ago', '%1$d hours ago', $hours), $hours);
  51. else
  52. $output = sprintf($hours == 1 ? '%1$d hour ago' : '%1$d hours ago', $hours);
  53. } else if ($age > 60*2) {
  54. $min = (int)($age/60);
  55. if ($resource)
  56. $output = sprintf($resource->ngettext('%1$d min ago', '%1$d min ago', $min), $min);
  57. else
  58. $output = sprintf($min == 1 ? '%1$d min ago' : '%1$d min ago', $min);
  59. } else if ($age > 2) {
  60. $sec = (int)$age;
  61. if ($resource)
  62. $output = sprintf($resource->ngettext('%1$d sec ago', '%1$d sec ago', $sec), $sec);
  63. else
  64. $output = sprintf($sec == 1 ? '%1$d sec ago' : '%1$d sec ago', $sec);
  65. } else {
  66. if ($resource)
  67. $output = $resource->translate('right now');
  68. else
  69. $output = 'right now';
  70. }
  71. if (!empty($params['assign']))
  72. $template->assign($params['assign'], $output);
  73. else
  74. return $output;
  75. }
  76. ?>