README.textile 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. Inspired by the comments at: "http://dk.php.net/time":http://dk.php.net/time and "http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695":http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695
  2. h1. Using this scheme to determine the "ago" time:
  3. <pre><code>
  4. 0 <-> 29 secs # => less than a minute
  5. 30 secs <-> 1 min, 29 secs # => 1 minute
  6. 1 min, 30 secs <-> 44 mins, 29 secs # => [2..44] minutes
  7. 44 mins, 30 secs <-> 89 mins, 29 secs # => about 1 hour
  8. 89 mins, 29 secs <-> 23 hrs, 59 mins, 29 secs # => about [2..24] hours
  9. 23 hrs, 59 mins, 29 secs <-> 47 hrs, 59 mins, 29 secs # => 1 day
  10. 47 hrs, 59 mins, 29 secs <-> 29 days, 23 hrs, 59 mins, 29 secs # => [2..29] days
  11. 29 days, 23 hrs, 59 mins, 30 secs <-> 59 days, 23 hrs, 59 mins, 29 secs # => about 1 month
  12. 59 days, 23 hrs, 59 mins, 30 secs <-> 1 yr minus 1 sec # => [2..12] months
  13. 1 yr <-> 2 yrs minus 1 secs # => about 1 year
  14. 2 yrs <-> max time or date # => over [2..X] years
  15. </code></pre>
  16. h1. Changes since last version
  17. Lots have changed since the last version. The WWDateTime class is no longer supported.
  18. I have implemented a new class which takes a time string as parameter (and a
  19. timezone if needed), and calculates the time between them.
  20. By request from "lsolesen" (a guy from here on github) I did removed the whole
  21. DateTime dependence thingy.
  22. h1. About
  23. This class is here to give you the same functionality that DateTime::diff will give you.
  24. However, this will work with PHP versions lower than 5.3.0
  25. This code is also available on Packagist: https://packagist.org/packages/jimmiw/php-time-ago
  26. h1. Two methods instead of just one!
  27. There are now two methods you can call: (well, 3, but two important ones!)
  28. <pre><code>
  29. $timeAgo = new TimeAgo();
  30. echo $timeAgo->inWords("2010/1/10 23:05:00");
  31. // OR
  32. echo timeAgoInWords("2010/1/10 23:05:00");
  33. </code></pre>
  34. Both methods gives the same answer, the "timeAgoInWords()" function is just a
  35. convenience wrapper for you.
  36. h1. Do you want the actual years, months, days, hours, minutes, seconds difference?
  37. Good news for you then!
  38. I've implemented a nice little function that does just that for you. Simply do the
  39. following:
  40. <pre><code>
  41. $timeAgo = new TimeAgo();
  42. $dateDifferenceArray = $timeAgo->dateDifference("2010/1/10 23:05:00");
  43. </code></pre>
  44. This will return an array with the following data:
  45. <pre><code>
  46. array(
  47. );
  48. </code></pre>
  49. h1. Translations added
  50. You can now translate the texts returned using the $timeAgo->inWords() or timeAgoInWords() methods.
  51. The translation is simply a language code string added to the end of the class init or timeAgoInWords() method.
  52. Examples using the Danish translations:
  53. <pre><code>
  54. $timeZone = NULL; // just use the system timezone
  55. $timeAgo = new TimeAgo($timeZone, 'da');
  56. echo $timeAgo->inWords("2010/1/10 23:05:00");
  57. // OR
  58. echo timeAgoInWords("2010/1/10 23:05:00", $timeZone, 'da');
  59. </code></pre>
  60. h1. Changes (versions)
  61. 0.4.15: Added Turkish translation (@esrefesen), and Thai (@jaideejung007) and iranian- Farsi(Persian) (@datamweb) and French (@lamberttraccard)
  62. 0.4.14: Added Arabic translation (@xc0d3rz)
  63. 0.4.13: @jmontoyaa fixed a timezone setting bug
  64. 0.4.12: Merged #18, #33 (you can now supply an alternative translations folder), #35 and Added Czech (@misaon)
  65. 0.4.11: Changes to French translation (@souhailmerroun). Added Russian and Ukranian translations (@akavolkol)
  66. 0.4.10: Added Korean and Finnish translations (thanks to @easylogic and @tk1sul1)
  67. 0.4.9: Added Spanish and Hungarian and Brazilian Portuguese (thanks to @khrizt and @technodelight and @gugoan)
  68. 0.4.8: Added French and Japanese and Taiwanese translations (thanks to @barohatoum and @hotengchang)
  69. 0.4.7: Changes to fix annoying "floor" bugs.
  70. 0.4.6: Added Dutch and Chinese translations (thanks to @NielsdeBlaauw and @su-xiaolin)
  71. 0.4.5: Fix for wrongly named variable - patch from @rimager
  72. 0.4.4: Changed the previous fix, since it caused a problem with the "Scheme" at the start of this document. "about 1 hour ago" is between 44mins30secs and 89mins29secs.
  73. 0.4.3: Added a fix for "about 1 hour ago", which would yield "1 hours ago" if the time was 1hour 30minutes. Giving a pluralization error.
  74. 0.4.2: Added German translations .
  75. 0.4.1: Added "ago" to the end of the translations, so the texts now read "about 1 hour ago".
  76. 0.4.0: Added translations (Danish and English) to the plugin.
  77. h1. The old way of doing things.
  78. Just if you wanted to try out the old "time ago" class, I've kept it in the project.
  79. It will eventually be deleted, but hey, go have fun with it. Perhaps even extend it etc.
  80. Bear in mind though, that the new DateTime::diff in PHP version 5.3.0 will eventually
  81. replace it completely. (PHP 5.3.0 DateTime::diff link: http://www.php.net/manual/en/datetime.diff.php)
  82. h1. Known bugs
  83. * dateDifference() calculates a bit wrong when finding differences over 1 year.
  84. h1. The MIT License
  85. Copyright © 2014 Jimmi Westerberg (http://westsworld.dk)
  86. Permission is hereby granted, free of charge, to any person obtaining a copy
  87. of this software and associated documentation files (the “Software”), to deal
  88. in the Software without restriction, including without limitation the rights
  89. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  90. copies of the Software, and to permit persons to whom the Software is
  91. furnished to do so, subject to the following conditions:
  92. The above copyright notice and this permission notice shall be included in
  93. all copies or substantial portions of the Software.
  94. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  95. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  96. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  97. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  98. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  99. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  100. THE SOFTWARE.