westsworld.datetime.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * NOTE: This class is no longer maintained as of version 0.2.0.
  4. * You should use the timeAgoInWords("date") convenience method instead.
  5. *
  6. * A specialization of the DateTime class, that can print out,
  7. * "how long ago" it was.
  8. *
  9. * @author jimmiw
  10. * @since 2009-09-28
  11. * @site http://github.com/jimmiw/php-time-ago
  12. */
  13. class WWDateTime extends DateTime {
  14. /**
  15. *
  16. */
  17. public function __construct(/*string*/ $time = "now",
  18. DateTimeZone $timezone = NULL) {
  19. // if the $timezone is null, we take 'Europe/London' as the default
  20. // this was done, because the parent construct tossed an exception
  21. if($timezone == NULL) {
  22. $timezone = new DateTimeZone('Europe/London');
  23. }
  24. // initializes the parant
  25. parent::__construct($time, $timezone);
  26. }
  27. /**
  28. * Returns how long time ago this date object is, in words.
  29. * E.g. current date could be "2009-01-02" and this object could hold
  30. * "2009-01-01", then this function would return "1 day".
  31. *
  32. * @return the number of time ago, in words (e.g. 2 days).
  33. */
  34. public function timeAgoInWords() {
  35. $timeAgoInWords = NULL;
  36. $yearDiff = 0;
  37. $monthDiff = 0;
  38. $dayDiff = 0;
  39. $hourDiff = 0;
  40. $minuteDiff = 0;
  41. $secondDiff = 0;
  42. $now = new DateTime("now", $this->getTimezone());
  43. // tests the years
  44. $yearDiff = $this->findDiff($now, $this, 'Y');
  45. if($yearDiff == 1) {
  46. $timeAgoInWords = $this->constructTimeAgoWord(
  47. $yearDiff,
  48. 'year',
  49. 1,
  50. 'about'
  51. );
  52. }
  53. else if($yearDiff >= 2) {
  54. $timeAgoInWords = $this->constructTimeAgoWord(
  55. $yearDiff,
  56. 'year',
  57. 1,
  58. 'over'
  59. );
  60. }
  61. // tests the months
  62. if($timeAgoInWords == NULL) {
  63. $timeAgoInWords = $this->constructTimeAgoWord(
  64. $this->findDiff($now, $this, 'n'),
  65. 'month',
  66. 1,
  67. 'about'
  68. );
  69. }
  70. // tests the days
  71. if($timeAgoInWords == NULL) {
  72. $timeAgoInWords = $this->constructTimeAgoWord(
  73. $this->findDiff($now, $this, 'j'),
  74. 'day',
  75. 1
  76. );
  77. }
  78. // tests the hours
  79. if($timeAgoInWords == NULL) {
  80. $timeAgoInWords = $this->constructTimeAgoWord(
  81. $this->findDiff($now, $this, 'G'),
  82. 'hour',
  83. 1,
  84. 'about'
  85. );
  86. }
  87. // tests the minutes
  88. if($timeAgoInWords == NULL) {
  89. $minuteDiff = $this->findDiff($now, $this, 'i');
  90. // if under 44 mins!
  91. if($minuteDiff <= 44) {
  92. $timeAgoInWords = $this->constructTimeAgoWord(
  93. $minuteDiff,
  94. 'minute',
  95. 1
  96. );
  97. }
  98. // else it's about an hour
  99. else {
  100. $timeAgoInWords = $this->constructTimeAgoWord(
  101. 1,
  102. 'hour',
  103. 1,
  104. 'about'
  105. );
  106. }
  107. }
  108. // tests the seconds
  109. if($timeAgoInWords == NULL) {
  110. $secondDiff = $this->findDiff($now, $this, 's');
  111. // if under 29 secs
  112. if($secondDiff <= 29) {
  113. $timeAgoInWords = "less than a minute";
  114. }
  115. // else it's a minute!
  116. else {
  117. $timeAgoInWords = $this->constructTimeAgoWord(
  118. 1,
  119. 'minute'
  120. );
  121. }
  122. }
  123. return $timeAgoInWords;
  124. }
  125. /**
  126. * Finds the difference in the two DateTime objects, using the given format.
  127. * @param from
  128. * @param to
  129. * @param format
  130. * @return the difference in the two DateTime objects
  131. */
  132. private function findDiff(DateTime $from = NULL,
  133. DateTime $to = NULL,
  134. $format = NULL) {
  135. return $from->format($format) - $to->format($format);
  136. }
  137. /**
  138. * Constructs the actual "time ago"-word
  139. * @param timeDifference
  140. * @param timeName
  141. * @param decidingTimeDifference
  142. * @param prefix
  143. * @param postfix
  144. * @return the "time ago"-word generated
  145. */
  146. private function constructTimeAgoWord($timeDiffrence = 0,
  147. $timeName = NULL,
  148. $decidingTimeDifference = 1,
  149. $prefix = NULL,
  150. $postfix = NULL) {
  151. // initializes the timeAgoInWord placeholder
  152. $timeAgoInWords = NULL;
  153. if($timeDiffrence > 0) {
  154. // sets the difference
  155. $timeAgoInWords = $timeDiffrence . " ";
  156. // adds the "prefix word", if any
  157. if($prefix != NULL) {
  158. // blindly adds a space between the words
  159. $timeAgoInWords = $prefix . " " . $timeAgoInWords;
  160. }
  161. // tests if we are to pluralize the time name or not
  162. if($timeDiffrence > $decidingTimeDifference) {
  163. $timeAgoInWords .= $this->pluralize($timeName);
  164. }
  165. else {
  166. $timeAgoInWords .= $timeName;
  167. }
  168. // adds the "postfix word", if any
  169. if($postfix != NULL) {
  170. // blindly adds a space between the words
  171. $timeAgoInWords .= " " . $postfix;
  172. }
  173. }
  174. // returns the "time ago in words" found, else NULL
  175. return $timeAgoInWords;
  176. }
  177. /**
  178. * Pluralizes the given word (only if it's in my list ofc!)
  179. * @param $word the word to pluralize
  180. * @return the pluralized word, if possible.
  181. */
  182. private function pluralize($word = NULL) {
  183. $pluralizedWord = $word;
  184. if($word == 'year') {
  185. $pluralizedWord = 'years';
  186. }
  187. else if($word == 'month') {
  188. $pluralizedWord = 'months';
  189. }
  190. else if($word == 'day') {
  191. $pluralizedWord = 'days';
  192. }
  193. else if($word == 'hour') {
  194. $pluralizedWord = 'hours';
  195. }
  196. else if($word == 'minute') {
  197. $pluralizedWord = 'minutes';
  198. }
  199. else if($word == 'second') {
  200. $pluralizedWord = 'seconds';
  201. }
  202. return $pluralizedWord;
  203. }
  204. }
  205. ?>