PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/libraries/Time.php

https://github.com/pmwalsh/dwa
PHP | 228 lines | 111 code | 56 blank | 61 comment | 19 complexity | dfa144a7a37654dee8eeb78901a90f0b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?
  2. class Time {
  3. /*-------------------------------------------------------------------------------------------------
  4. -------------------------------------------------------------------------------------------------*/
  5. public static function offset($remote, $local = NULL, $now = NULL) {
  6. if ($local === NULL) {
  7. # Use the default timezone
  8. $local = date_default_timezone_get();
  9. }
  10. if (is_int($now)) {
  11. # Convert the timestamp into a string
  12. $now = date(DateTime::RFC2822, $now);
  13. }
  14. # Create timezone objects
  15. $zone_remote = new DateTimeZone($remote);
  16. $zone_local = new DateTimeZone($local);
  17. # Create date objects from timezones
  18. $time_remote = new DateTime($now, $zone_remote);
  19. $time_local = new DateTime($now, $zone_local);
  20. # Find the offset
  21. $offset = $zone_remote->getOffset($time_remote) - $zone_local->getOffset($time_local);
  22. return $offset;
  23. }
  24. /*-----------------------------------------------------------------------------------------------
  25. Always use this method instead of date() or time() to find out the current time so
  26. we are able to use the MIMIC_TIME feature when testing.
  27. -------------------------------------------------------------------------------------------------*/
  28. public static function now() {
  29. # Do the test with strtotime, otherwise it reads constants weird if they don't exist and this doesn't work right
  30. if(@strtotime(MIMIC_TIME) > 0) {
  31. return strtotime(MIMIC_TIME);
  32. }
  33. else {
  34. return time();
  35. }
  36. }
  37. /*-------------------------------------------------------------------------------------------------
  38. $timestamp is usually application time, $timezone is usually the user's time
  39. -------------------------------------------------------------------------------------------------*/
  40. public static function display($timestamp, $time_format = NULL, $timezone = NULL) {
  41. # Avoid printing December 31 1969 if there is no timestamp
  42. if($timestamp <= 0) {
  43. return "";
  44. }
  45. else {
  46. # Priority of time format
  47. # 1. If it's passed in
  48. # 2. The App's TIME_FORMAT setting
  49. # 3. A default value we set here
  50. if($time_format) {
  51. }
  52. elseif(TIME_FORMAT) {
  53. $time_format = TIME_FORMAT;
  54. }
  55. else {
  56. $time_format = "F j, Y g:ia";
  57. }
  58. # If passed in a timezone, use that one
  59. if (!empty($timezone)) {
  60. $timestamp = $timestamp + self::offset($timezone);
  61. }
  62. return date($time_format, $timestamp);
  63. }
  64. }
  65. /*-------------------------------------------------------------------------------------------------
  66. Given a timestamp, returns an english string with relative time like "2 hours ago"
  67. -------------------------------------------------------------------------------------------------*/
  68. public static function time_ago($datefrom, $dateto=-1) {
  69. // Defaults and assume if 0 is passed in that
  70. // its an error rather than the epoch
  71. if($datefrom<=0) { return "A long time ago"; }
  72. if($dateto==-1) { $dateto = time(); }
  73. // Calculate the difference in seconds betweeen
  74. // the two timestamps
  75. $difference = $dateto - $datefrom;
  76. // If difference is less than 60 seconds,
  77. // seconds is a good interval of choice
  78. if($difference < 60) {
  79. $interval = "s";
  80. }
  81. // If difference is between 60 seconds and
  82. // 60 minutes, minutes is a good interval
  83. elseif($difference >= 60 && $difference<60*60) {
  84. $interval = "n";
  85. }
  86. // If difference is between 1 hour and 24 hours
  87. // hours is a good interval
  88. elseif($difference >= 60*60 && $difference<60*60*24) {
  89. $interval = "h";
  90. }
  91. // If difference is between 1 day and 7 days
  92. // days is a good interval
  93. elseif($difference >= 60*60*24 && $difference<60*60*24*7) {
  94. $interval = "d";
  95. }
  96. // If difference is between 1 week and 30 days
  97. // weeks is a good interval
  98. elseif($difference >= 60*60*24*7 && $difference < 60*60*24*30) {
  99. $interval = "ww";
  100. }
  101. // If difference is between 30 days and 365 days
  102. // months is a good interval, again, the same thing
  103. // applies, if the 29th February happens to exist
  104. // between your 2 dates, the function will return
  105. // the 'incorrect' value for a day
  106. elseif($difference >= 60*60*24*30 && $difference < 60*60*24*365) {
  107. $interval = "m";
  108. }
  109. // If difference is greater than or equal to 365
  110. // days, return year. This will be incorrect if
  111. // for example, you call the function on the 28th April
  112. // 2008 passing in 29th April 2007. It will return
  113. // 1 year ago when in actual fact (yawn!) not quite
  114. // a year has gone by
  115. elseif($difference >= 60*60*24*365) {
  116. $interval = "y";
  117. }
  118. // Based on the interval, determine the
  119. // number of units between the two dates
  120. // From this point on, you would be hard
  121. // pushed telling the difference between
  122. // this function and DateDiff. If the $datediff
  123. // returned is 1, be sure to return the singular
  124. // of the unit, e.g. 'day' rather 'days'
  125. switch($interval) {
  126. case "m":
  127. $months_difference = floor($difference / 60 / 60 / 24 / 29);
  128. while (mktime(date("H", $datefrom), date("i", $datefrom),
  129. date("s", $datefrom), date("n", $datefrom)+($months_difference),
  130. date("j", $dateto), date("Y", $datefrom)) < $dateto)
  131. {
  132. $months_difference++;
  133. }
  134. $datediff = $months_difference;
  135. // We need this in here because it is possible
  136. // to have an 'm' interval and a months
  137. // difference of 12 because we are using 29 days
  138. // in a month
  139. if($datediff==12) {
  140. $datediff--;
  141. }
  142. $res = ($datediff==1) ? "$datediff month ago" : "$datediff months ago";
  143. break;
  144. case "y":
  145. $datediff = floor($difference / 60 / 60 / 24 / 365);
  146. $res = ($datediff==1) ? "$datediff year ago" : "$datediff years ago";
  147. break;
  148. case "d":
  149. $datediff = floor($difference / 60 / 60 / 24);
  150. $res = ($datediff==1) ? "$datediff day ago" : "$datediff days ago";
  151. break;
  152. case "ww":
  153. $datediff = floor($difference / 60 / 60 / 24 / 7);
  154. $res = ($datediff==1) ? "$datediff week ago" : "$datediff weeks ago";
  155. break;
  156. case "h":
  157. $datediff = floor($difference / 60 / 60);
  158. $res = ($datediff==1) ? "$datediff hour ago" : "$datediff hours ago";
  159. break;
  160. case "n":
  161. $datediff = floor($difference / 60);
  162. $res = ($datediff==1) ? "$datediff min ago" : "$datediff mins ago";
  163. break;
  164. case "s":
  165. $datediff = $difference;
  166. $res = ($datediff==1) ? "$datediff second ago" : "$datediff seconds ago";
  167. break;
  168. }
  169. return $res;
  170. }
  171. } # eoc