test.datetime.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. require('../westsworld.datetime.class.php');
  3. require('../timeago.inc.php');
  4. echo "<h1>TimeAgo class tests!</h1>";
  5. echo "<p>";
  6. echo "<h2>testing TimeAgo class</h2>";
  7. echo "<strong>rule 1</strong> (0 <-> 29 secs # => less than a minute)<br/>";
  8. $timeAgo = new TimeAgo();
  9. echo $timeAgo->inWords("2010/4/26 22:03:29","2010/4/26 22:03:58");
  10. echo "<br/>";
  11. echo "<br/><strong>rule 2</strong> (30 secs <-> 1 min, 29 secs # => 1 minute)<br/>";
  12. $timeAgo = new TimeAgo();
  13. echo $timeAgo->inWords("2010/4/26 22:03:30","2010/4/26 22:04:58");
  14. echo "<br/>";
  15. echo "<br/><strong>rule 3</strong> (1 min, 30 secs <-> 44 mins, 29 secs # => [2..44] minutes)<br/>";
  16. $timeAgo = new TimeAgo();
  17. echo $timeAgo->inWords("2010/4/26 22:03:28","2010/4/26 22:04:58");
  18. echo "<br/>";
  19. $timeAgo = new TimeAgo();
  20. echo $timeAgo->inWords("2010/4/26 22:03:28","2010/4/26 22:15:58");
  21. echo "<br/>";
  22. $timeAgo = new TimeAgo();
  23. echo $timeAgo->inWords("2010/4/26 22:03:28","2010/4/26 22:47:57");
  24. echo "<br/>";
  25. echo "<br/><strong>rule 4</strong> (44 mins, 30 secs <-> 89 mins, 29 secs # => about 1 hour)<br/>";
  26. $timeAgo = new TimeAgo();
  27. echo $timeAgo->inWords("2010/4/26 22:03:28","2010/4/26 22:47:58");
  28. echo "<br/>";
  29. $timeAgo = new TimeAgo();
  30. echo $timeAgo->inWords("2010/4/26 22:03:28","2010/4/26 23:32:57");
  31. echo "<br/>";
  32. echo "<br/><strong>rule 5</strong>(89 mins, 29 secs <-> 23 hrs, 59 mins, 29 secs # => about [2..24] hours)<br/>";
  33. $timeAgo = new TimeAgo();
  34. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/26 01:30:00");
  35. echo "<br/>";
  36. $timeAgo = new TimeAgo();
  37. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/26 13:49:00");
  38. echo "<br/>";
  39. $timeAgo = new TimeAgo();
  40. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/26 23:59:29");
  41. echo "<br/>";
  42. echo "<br/><strong>rule 6</strong> (23 hrs, 59 mins, 29 secs <-> 47 hrs, 59 mins, 29 secs # => 1 day)<br/>";
  43. $timeAgo = new TimeAgo();
  44. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/26 23:59:30");
  45. echo "<br/>";
  46. $timeAgo = new TimeAgo();
  47. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/27 13:10:00");
  48. echo "<br/>";
  49. $timeAgo = new TimeAgo();
  50. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/27 23:59:29");
  51. echo "<br/>";
  52. echo "<br/><strong>rule 7</strong> (47 hrs, 59 mins, 30 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days)<br/>";
  53. $timeAgo = new TimeAgo();
  54. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/4/27 23:59:30");
  55. echo "<br/>";
  56. $timeAgo = new TimeAgo();
  57. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/5/10 00:00:00");
  58. echo "<br/>";
  59. $timeAgo = new TimeAgo();
  60. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/5/25 23:59:29");
  61. echo "<br/>";
  62. echo "<br/><strong>rule 8</strong> (29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month)<br/>";
  63. $timeAgo = new TimeAgo();
  64. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/5/25 23:59:30");
  65. echo "<br/>";
  66. $timeAgo = new TimeAgo();
  67. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/5/28 10:05:30");
  68. echo "<br/>";
  69. $timeAgo = new TimeAgo();
  70. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/6/24 23:59:29");
  71. echo "<br/>";
  72. echo "<br/><strong>rule 9</strong> (59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months)<br/>";
  73. $timeAgo = new TimeAgo();
  74. echo $timeAgo->inWords("2010/4/26 00:00:00","2010/6/24 23:59:30");
  75. echo "<br/>";
  76. $timeAgo = new TimeAgo();
  77. echo $timeAgo->inWords("2009/10/01 00:00:00","2010/5/28 10:05:30");
  78. echo "<br/>";
  79. $timeAgo = new TimeAgo();
  80. echo $timeAgo->inWords("2009/4/26 00:00:00","2010/4/20 00:00:00");
  81. echo "<br/>";
  82. echo "<br/><strong>rule 10</strong> (1 yr <-> 2 yrs minus 1 secs # => about 1 year)<br/>";
  83. $timeAgo = new TimeAgo();
  84. echo $timeAgo->inWords("2009/4/26 00:00:00","2010/4/26 00:00:00");
  85. echo "<br/>";
  86. $timeAgo = new TimeAgo();
  87. echo $timeAgo->inWords("2009/01/01 00:00:00","2010/5/01 00:00:00");
  88. echo "<br/>";
  89. $timeAgo = new TimeAgo();
  90. echo $timeAgo->inWords("2010/4/26 00:00:00","2011/4/26 23:59:59");
  91. echo "<br/>";
  92. echo "<br/><strong>rule 11</strong> (2 yrs <-> max time or date # => over [2..X] years)<br/>";
  93. $timeAgo = new TimeAgo();
  94. echo $timeAgo->inWords("2009/4/26 00:00:00","2011/4/26 00:00:00");
  95. echo "<br/>";
  96. $timeAgo = new TimeAgo();
  97. echo $timeAgo->inWords("2005/4/26 00:00:00","2011/4/26 00:00:00");
  98. echo "<br/>";
  99. $timeAgo = new TimeAgo();
  100. echo $timeAgo->inWords("1999/4/26 00:00:00","2011/4/26 00:00:00");
  101. echo "<br/>";
  102. echo "</p>";
  103. echo "<h2>TimeAgo class tests (dateDifference)</h2>";
  104. echo "<p>";
  105. $timeAgo = new TimeAgo();
  106. echo "<pre>";
  107. print_r($timeAgo->dateDifference("2010/4/01 00:00:00","2010/5/12 03:05:30"));
  108. echo "</pre>";
  109. echo "</p>";
  110. echo "<h1>WWDateTime class tests!</h1>";
  111. function test_time($timeAgo, $timeAsItShouldBe) {
  112. echo "<p>";
  113. $datetime = new WWDateTime($timeAgo);
  114. echo $datetime->format(DATE_RFC3339);
  115. echo " = ";
  116. echo $datetime->timeAgoInWords();
  117. echo " === ";
  118. echo $timeAsItShouldBe;
  119. echo "</p>";
  120. }
  121. test_time("-2 year", "over 2 years");
  122. timeAgoInWords("-2 year");
  123. test_time("-1 year", "about 1 year");
  124. timeAgoInWords("-1 year");
  125. test_time("-1 month", "about 1 month");
  126. timeAgoInWords("-1 month");
  127. test_time("-2 month", "about 2 months");
  128. timeAgoInWords("-2 month");
  129. test_time("-1 day", "1 day");
  130. timeAgoInWords("-1 day");
  131. test_time("-2 day", "2 days");
  132. timeAgoInWords("-2 day");
  133. test_time("-1 hour", "about 1 hour");
  134. timeAgoInWords("-1 hour");
  135. test_time("-2 hour", "about 2 hours");
  136. timeAgoInWords("-2 hour");
  137. test_time("-1 minute", "about 1 minute");
  138. timeAgoInWords("-1 minute");
  139. test_time("-2 minute", "about 2 minutes");
  140. timeAgoInWords("-2 minute");
  141. test_time("-44 minute", "about 44 minutes");
  142. timeAgoInWords("-44 minute");
  143. test_time("-45 minute", "about 1 hour");
  144. timeAgoInWords("-45 minute");
  145. test_time("-1 second", "less than a minute");
  146. timeAgoInWords("-1 second");
  147. test_time("-31 second", "1 minute");
  148. timeAgoInWords("-31 second");
  149. echo "<h2>Language testing</h2>";
  150. echo "<p>";
  151. echo "<br/><strong>English</strong><br/>";
  152. $timeAgo = new TimeAgo();
  153. echo $timeAgo->inWords("2015/5/26 10:00:10","2015/5/26 10:00:20");
  154. echo "<br/>";
  155. echo "<br/><strong>Danish</strong><br/>";
  156. $timeAgo = new TimeAgo(NULL, 'da');
  157. echo $timeAgo->inWords("2015/5/26 10:00:10","2015/5/26 10:00:20");
  158. echo "<br/>";
  159. echo "</p>";
  160. ?>