human-time.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * Humanized timestamps
  17. */
  18. function human_timestamp ($unix_timestamp, $now = null) {
  19. if (is_null($now)) {
  20. $now = time();
  21. }
  22. $diff = $now - $unix_timestamp;
  23. switch ($unix_timestamp) {
  24. case ($now < $unix_timestamp):
  25. return _('in the future (?)');
  26. break;
  27. case ($diff == 1):
  28. # one second
  29. return _('a second ago');
  30. break;
  31. case ($diff < 60):
  32. # less than a minute
  33. return sprintf('%d seconds ago', $diff);
  34. break;
  35. case ($diff < 120):
  36. # between a minute and two
  37. return _('about a minute ago');
  38. break;
  39. case ($diff < 3600):
  40. # less than an hour
  41. return sprintf(_('%d minutes ago'), round($diff / 60));
  42. break;
  43. case ($diff < 7200):
  44. # between an hour and two
  45. return _('about an hour ago');
  46. break;
  47. case ($diff < 86400):
  48. # less than a day
  49. return sprintf(_('%d hours ago'), round($diff / 3600));
  50. break;
  51. case ($diff < 172800):
  52. # less than two days
  53. return _('about a day ago');
  54. break;
  55. case ($diff < 604800):
  56. # less than a week
  57. if (round($diff / 86400) == 7) {
  58. return _('about a week ago');
  59. }
  60. return sprintf(_('%d days ago'), round($diff / 86400));
  61. break;
  62. case ($diff < 691200):
  63. # a week an a day
  64. return _('about a week ago');
  65. break;
  66. case ($diff < 2764800):
  67. # less than a month
  68. if (round($diff / 691200) == 1) {
  69. return _('about a week ago');
  70. }
  71. return sprintf(_('%d weeks ago'), round($diff / 691200));
  72. break;
  73. case ($diff < 4579200):
  74. # a month and three weeks
  75. return _('about a month ago');
  76. break;
  77. case ($diff < 33177600);
  78. # less than a year
  79. return sprintf(_('%d months ago'), round($diff / 2764800));
  80. break;
  81. case ($diff < 35942400):
  82. # a year and a month
  83. return _('about a year ago');
  84. break;
  85. case ($diff > 35942400):
  86. return _('more than a year ago');
  87. break;
  88. }
  89. }