PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/social/lib/social/date.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 401 lines | 271 code | 47 blank | 83 comment | 60 complexity | 7cb07df46d6e9b6c1301750a7915333b MD5 | raw file
  1. <?php
  2. /**
  3. * Date helper.
  4. *
  5. * This helper is originally Kohana_Date in the Kohana Framework. This has been renamed and modified for Social's needs.
  6. *
  7. * @package Kohana
  8. * @category Helpers
  9. * @author Kohana Team
  10. * @copyright (c) 2007-2011 Kohana Team
  11. * @license http://kohanaframework.org/license
  12. */
  13. class Social_Date {
  14. // Second amounts for various time increments
  15. const YEAR = 31556926;
  16. const MONTH = 2629744;
  17. const WEEK = 604800;
  18. const DAY = 86400;
  19. const HOUR = 3600;
  20. const MINUTE = 60;
  21. /**
  22. * Returns time difference between two timestamps, in human readable format.
  23. * If the second timestamp is not given, the current time will be used.
  24. * Also consider using [self::fuzzy_span] when displaying a span.
  25. *
  26. * $span = self::span(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
  27. * $span = self::span(60, 182, 'minutes'); // 2
  28. *
  29. * @param integer timestamp to find the span of
  30. * @param integer timestamp to use as the baseline
  31. * @param string formatting string
  32. * @return string when only a single output is requested
  33. * @return array associative list of all outputs requested
  34. */
  35. public static function span($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds') {
  36. // Normalize output
  37. $output = trim(strtolower((string)$output));
  38. if (!$output) {
  39. // Invalid output
  40. return FALSE;
  41. }
  42. // Array with the output formats
  43. $output = preg_split('/[^a-z]+/', $output);
  44. // Convert the list of outputs to an associative array
  45. $output = array_combine($output, array_fill(0, count($output), 0));
  46. // Make the output values into keys
  47. extract(array_flip($output), EXTR_SKIP);
  48. if ($local === NULL) {
  49. // Calculate the span from the current time
  50. $local = time();
  51. }
  52. // Calculate timespan (seconds)
  53. $timespan = abs($remote - $local);
  54. if (isset($output['years'])) {
  55. $timespan -= self::YEAR * ($output['years'] = (int)floor($timespan / self::YEAR));
  56. }
  57. if (isset($output['months'])) {
  58. $timespan -= self::MONTH * ($output['months'] = (int)floor($timespan / self::MONTH));
  59. }
  60. if (isset($output['weeks'])) {
  61. $timespan -= self::WEEK * ($output['weeks'] = (int)floor($timespan / self::WEEK));
  62. }
  63. if (isset($output['days'])) {
  64. $timespan -= self::DAY * ($output['days'] = (int)floor($timespan / self::DAY));
  65. }
  66. if (isset($output['hours'])) {
  67. $timespan -= self::HOUR * ($output['hours'] = (int)floor($timespan / self::HOUR));
  68. }
  69. if (isset($output['minutes'])) {
  70. $timespan -= self::MINUTE * ($output['minutes'] = (int)floor($timespan / self::MINUTE));
  71. }
  72. // Seconds ago, 1
  73. if (isset($output['seconds'])) {
  74. $output['seconds'] = $timespan;
  75. }
  76. if (count($output) === 1) {
  77. // Only a single output was requested, return it
  78. return array_pop($output);
  79. }
  80. // Return array
  81. return $output;
  82. }
  83. /**
  84. * Returns a formatted span.
  85. *
  86. * @static
  87. * @param string $remote
  88. * @param string $local
  89. * @return string
  90. */
  91. public static function span_formatted($remote, $local = NULL) {
  92. if ($local === NULL) {
  93. $local = current_time('timestamp', 1);
  94. }
  95. $span = self::span($remote, $local);
  96. $timespan = abs($remote - $local);
  97. $output = '';
  98. // Years
  99. if (!empty($span['years'])) {
  100. if ($span['years'] == '1') {
  101. $output = __('1 year', 'social');
  102. }
  103. else {
  104. $output = sprintf(__('%s years', 'social'), $span['years']);
  105. }
  106. }
  107. // Months
  108. elseif (!empty($span['months'])) {
  109. if ($span['months'] == '1') {
  110. $output = __('1 month', 'social');
  111. }
  112. else {
  113. $output = sprintf(__('%s months', 'social'), $span['months']);
  114. }
  115. }
  116. // Weeks
  117. elseif (!empty($span['weeks'])) {
  118. if ($span['weeks'] == '1') {
  119. $output = __('1 week', 'social');
  120. }
  121. else {
  122. $output = sprintf(__('%s weeks', 'social'), $span['weeks']);
  123. }
  124. }
  125. // Days
  126. elseif (!empty($span['days'])) {
  127. if ($span['days'] == '1') {
  128. $output = __('1 day', 'social');
  129. }
  130. else {
  131. $output = sprintf(__('%s days', 'social'), $span['days']);
  132. }
  133. }
  134. // Hours and Minutes
  135. elseif (!empty($span['hours']) || !empty($span['minutes'])) {
  136. $hours = '';
  137. $minutes = '';
  138. if (!empty($span['hours'])) {
  139. if ($span['hours'] == '1') {
  140. $hours = __('1 hour', 'social');
  141. }
  142. else {
  143. $hours = sprintf(__('%s hours', 'social'), $span['hours']);
  144. }
  145. }
  146. if (!empty($span['minutes'])) {
  147. if ($span['minutes'] == '1') {
  148. $minutes = __('1 minute', 'social');
  149. }
  150. else {
  151. $minutes = sprintf(__('%s minutes', 'social'), $span['minutes']);
  152. }
  153. }
  154. if (!empty($hours) and !empty($minutes)) {
  155. $output = $hours.' and '.$minutes;
  156. }
  157. else if (!empty($hours)) {
  158. $output = $hours;
  159. }
  160. else {
  161. $output = $minutes;
  162. }
  163. }
  164. // Seconds
  165. elseif (!empty($span['seconds'])) {
  166. if ($span['seconds'] == '1') {
  167. $output = __('1 second', 'social');
  168. }
  169. else {
  170. $output = sprintf(__('%s seconds', 'social'), $span['seconds']);
  171. }
  172. }
  173. return apply_filters('social_formatted_date', $output, $remote, $local);
  174. }
  175. /**
  176. * Returns a formatted span for comments. Uses absolute date after 1 year.
  177. *
  178. * @static
  179. * @param string $remote
  180. * @param string $local
  181. * @return string
  182. */
  183. public static function span_comment($remote, $local = NULL) {
  184. if ($local === NULL) {
  185. $local = current_time('timestamp', 1);
  186. }
  187. $span = self::span($remote, $local);
  188. $timespan = abs($remote - $local);
  189. $output = '';
  190. // Years
  191. if (!empty($span['years'])) {
  192. $output = get_comment_date();
  193. }
  194. // Months
  195. elseif (!empty($span['months'])) {
  196. if ($span['months'] == '1') {
  197. $output = __('1 month ago', 'social');
  198. }
  199. else {
  200. $output = sprintf(__('%s months ago', 'social'), $span['months']);
  201. }
  202. }
  203. // Weeks
  204. elseif (!empty($span['weeks'])) {
  205. if ($span['weeks'] == '1') {
  206. $output = __('1 week ago', 'social');
  207. }
  208. else {
  209. $output = sprintf(__('%s weeks ago', 'social'), $span['weeks']);
  210. }
  211. }
  212. // Days
  213. elseif (!empty($span['days'])) {
  214. if ($span['days'] == '1') {
  215. $output = __('1 day ago', 'social');
  216. }
  217. else {
  218. $output = sprintf(__('%s days ago', 'social'), $span['days']);
  219. }
  220. }
  221. // Hours
  222. elseif (!empty($span['hours'])) {
  223. if ($span['hours'] == '1') {
  224. $output = __('1 hour ago', 'social');
  225. }
  226. else {
  227. $output = sprintf(__('%s hours', 'social'), $span['hours']);
  228. }
  229. }
  230. // Minutes
  231. elseif (!empty($span['minutes'])) {
  232. if ($span['minutes'] == '1') {
  233. $output = __('1 minute ago', 'social');
  234. }
  235. else {
  236. $output = sprintf(__('%s minutes ago', 'social'), $span['minutes']);
  237. }
  238. }
  239. // Seconds
  240. else {
  241. $output = __('just now', 'social');
  242. }
  243. return apply_filters('social_comment_date', $output, $remote, $local);
  244. }
  245. /**
  246. * Returns the difference between a time and now in a "fuzzy" way.
  247. * Displaying a fuzzy time instead of a date is usually faster to read and understand.
  248. *
  249. * $span = self::fuzzy_span(time() - 10); // "moments ago"
  250. * $span = self::fuzzy_span(time() + 20); // "in moments"
  251. *
  252. * A second parameter is available to manually set the "local" timestamp,
  253. * however this parameter shouldn't be needed in normal usage and is only
  254. * included for unit tests
  255. *
  256. * @param integer "remote" timestamp
  257. * @param integer "local" timestamp, defaults to time()
  258. * @return string
  259. */
  260. public static function fuzzy_span($timestamp, $local_timestamp = NULL) {
  261. $local_timestamp = ($local_timestamp === NULL) ? time() : (int)$local_timestamp;
  262. // Determine the difference in seconds
  263. $offset = abs($local_timestamp - $timestamp);
  264. if ($offset <= self::MINUTE) {
  265. $span = __('a few moments', 'social');
  266. }
  267. elseif ($offset < (self::MINUTE * 20))
  268. {
  269. $span = __('a few minutes', 'social');
  270. }
  271. elseif ($offset < self::HOUR)
  272. {
  273. $span = __('less than an hour', 'social');
  274. }
  275. elseif ($offset < (self::HOUR * 4))
  276. {
  277. $span = __('a couple of hours', 'social');
  278. }
  279. elseif ($offset < self::DAY)
  280. {
  281. $span = __('less than a day', 'social');
  282. }
  283. elseif ($offset < (self::DAY * 2))
  284. {
  285. $span = __('about a day', 'social');
  286. }
  287. elseif ($offset < (self::DAY * 4))
  288. {
  289. $span = __('a couple of days', 'social');
  290. }
  291. elseif ($offset < self::WEEK)
  292. {
  293. $span = __('less than a week', 'social');
  294. }
  295. elseif ($offset < (self::WEEK * 2))
  296. {
  297. $span = __('about a week', 'social');
  298. }
  299. elseif ($offset < self::MONTH)
  300. {
  301. $span = __('less than a month', 'social');
  302. }
  303. elseif ($offset < (self::MONTH * 2))
  304. {
  305. $span = __('about a month', 'social');
  306. }
  307. elseif ($offset < (self::MONTH * 4))
  308. {
  309. $span = __('a couple of months', 'social');
  310. }
  311. elseif ($offset < self::YEAR)
  312. {
  313. $span = __('less than a year', 'social');
  314. }
  315. elseif ($offset < (self::YEAR * 2))
  316. {
  317. $span = __('about a year', 'social');
  318. }
  319. elseif ($offset < (self::YEAR * 4))
  320. {
  321. $span = __('a couple of years', 'social');
  322. }
  323. elseif ($offset < (self::YEAR * 8))
  324. {
  325. $span = __('a few years', 'social');
  326. }
  327. elseif ($offset < (self::YEAR * 12))
  328. {
  329. $span = __('about a decade', 'social');
  330. }
  331. elseif ($offset < (self::YEAR * 24))
  332. {
  333. $span = __('a couple of decades', 'social');
  334. }
  335. elseif ($offset < (self::YEAR * 64))
  336. {
  337. $span = __('several decades', 'social');
  338. }
  339. else
  340. {
  341. $span = __('a long time', 'social');
  342. }
  343. if ($timestamp <= $local_timestamp) {
  344. // This is in the past
  345. $output = sprintf(__('%s ago', 'social'), $span);
  346. }
  347. else
  348. {
  349. // This in the future
  350. $output = sprintf(__('in %s', 'social'), $span);
  351. }
  352. return apply_filters('social_fuzzy_date', $output, $remote, $local);
  353. }
  354. } // End Social_Date