PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/saf/lib/Date/Date.php

https://github.com/cbrunet/sitellite
PHP | 481 lines | 269 code | 14 blank | 198 comment | 71 complexity | 7fc71fc397911f612e943a4df5d6fa14 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | Sitellite - Content Management System |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2007 Simian Systems |
  7. // +----------------------------------------------------------------------+
  8. // | This software is released under the GNU General Public License (GPL) |
  9. // | Please see the accompanying file docs/LICENSE for licensing details. |
  10. // | |
  11. // | You should have received a copy of the GPL Software License along |
  12. // | with this program; if not, write to Simian Systems, 242 Lindsay, |
  13. // | Winnipeg, MB, R3N 1H1, CANADA. The License is also available at |
  14. // | the following web site address: |
  15. // | <http://www.sitellite.org/index/license> |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: John Luxford <lux@simian.ca> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // Date is a module that contains methods to format and generate dates.
  21. //
  22. define ('YEAR_IN_SECONDS', 31536000);
  23. define ('WEEK_IN_SECONDS', 604800);
  24. define ('DAY_IN_SECONDS', 86400);
  25. define ('HOUR_IN_SECONDS', 3600);
  26. /**
  27. * A class used to format dates, as well as generate them.
  28. *
  29. * New in 1.4:
  30. * - format(), time(), and timestamp() methods don't error on 00000... date values.
  31. *
  32. * New in 1.6:
  33. * - Added add(), subtract(), and roundTime() methods.
  34. *
  35. * New in 1.8:
  36. * - You can now pass an associative array of formats to the format() and
  37. * timestamp() methods. The keys may be 'today', 'yesterda', 'tomorrow',
  38. * 'this week', and 'other'. The appropriate format will then be used.
  39. *
  40. * New in 2.0:
  41. * - Added a toUnix() method, a compare() method, and improved the flexibility
  42. * of the basic format() method to support the formats of timestamp() as well.
  43. * - This package now defines four constants, which contain the following
  44. * values:
  45. * - YEAR_IN_SECONDS, 31536000
  46. * - WEEK_IN_SECONDS, 604800
  47. * - DAY_IN_SECONDS, 86400
  48. * - HOUR_IN_SECONDS, 3600
  49. *
  50. * New in 2.2:
  51. * - Removed the EasyText() and EasyTextInit() methods.
  52. *
  53. * <code>
  54. * <?php
  55. *
  56. * // print the current date
  57. * echo Date::format ();
  58. *
  59. * ? >
  60. * </code>
  61. *
  62. * @package Date
  63. * @author John Luxford <lux@simian.ca>
  64. * @copyright Copyright (C) 2001-2003, Simian Systems Inc.
  65. * @license http://www.sitellite.org/index/license Simian Open Software License
  66. * @version 2.2, 2003-05-23, $Id: Date.php,v 1.6 2008/03/15 23:44:31 lux Exp $
  67. * @access public
  68. *
  69. */
  70. class Date {
  71. /**
  72. * Accepts a date ('Y-m-d' format), full date/time ('YmdHis' or 'Y-m-d H:i:s'
  73. * formats), or unix timestamp, and returns a unix timestamp equivalent (or the value
  74. * passed if passed a timestamp already). Returns a timestamp of the current
  75. * date/time if passed no date at all.
  76. *
  77. * @access public
  78. * @param mixed $date
  79. * @return integer
  80. *
  81. */
  82. function toUnix ($date = '') {
  83. if (empty ($date)) {
  84. return time ();
  85. } elseif (preg_match ('/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $date, $regs)) {
  86. // ordinary date, Y-m-d
  87. return mktime (0, 0, 0, $regs[2], $regs[3], $regs[1]);
  88. } elseif (preg_match ('/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/', $date, $regs)) {
  89. // formatted date & time, Y-m-d H:i:s
  90. return mktime ($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  91. } elseif (preg_match ('/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/', $date, $regs)) {
  92. // unformatted date & time, YmdHis
  93. return mktime ($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  94. } else {
  95. // it's already a timestamp
  96. return $date;
  97. }
  98. }
  99. /**
  100. * Compares two dates, full date/time ('YmdHis' or 'Y-m-d H:i:s' formats),
  101. * or unix timestamps, and returns -1 if the first is greater, 0 if they are equal,
  102. * and 1 if the second is greater. Optional third and fourth parameters,
  103. * $equality_range, and $range_forward, allow you to specify a range of time
  104. * in seconds that qualifies as the equality range. If $range_forward is set,
  105. * then $equality_range acts as a "past range" and $range_forward acts as a
  106. * "future" range, allowing you to compare a date to say the following 24 hours.
  107. * If $range_forward is not set, then $equality_range acts as both the past and
  108. * future range. Note: The range is always surrounding $date2, so for logic that
  109. * requires the range to be around $date1, reverse the dates.
  110. *
  111. * @access public
  112. * @param mixed $date1
  113. * @param mixed $date2
  114. * @param integer $equality_range
  115. * @param integer $range_forward
  116. * @return integer
  117. *
  118. */
  119. function compare ($date1, $date2, $equality_range = 0, $range_forward = false) {
  120. // returns -1 if first is greater
  121. // returns 0 if both are same
  122. // returns 1 if second is greater
  123. $one = Date::toUnix ($date1);
  124. $two = Date::toUnix ($date2);
  125. if ($range_forward === false) {
  126. $range_forward = $equality_range;
  127. }
  128. if ($one >= ($two - $equality_range) && $one <= ($two + $range_forward)) {
  129. return 0;
  130. } elseif ($one > $two) {
  131. return -1;
  132. } elseif ($two > $one) {
  133. return 1;
  134. }
  135. }
  136. /**
  137. * Returns the specified time (or the current time, if unspecified) with
  138. * an offset in hours from GMT to the local timezone. $date is a Unix timestamp
  139. * in this method.
  140. *
  141. * @access public
  142. * @param string $date
  143. * @param integer $offset
  144. * @param string $format
  145. * @return string
  146. *
  147. */
  148. function local ($date = '', $offset = 0, $format = 'F j, Y h:i:s a') {
  149. if (! empty ($date)) {
  150. return gmdate ($format, $date + ($offset * 60 * 60));
  151. } else {
  152. return gmdate ($format, time () + ($offset * 60 * 60));
  153. }
  154. }
  155. /**
  156. * Formats a date provided in ISO format (YYYY-MM-DD) in the new
  157. * format specified.
  158. *
  159. * @access public
  160. * @param string $date
  161. * @param string $format
  162. * @return string
  163. *
  164. */
  165. function format ($date = '', $format = 'F j, Y') {
  166. /*if (empty ($date)) {
  167. $date = date ('Y-m-d');
  168. }
  169. if ($date == '0000-00-00') {
  170. return 'Empty';
  171. }
  172. list ($y, $m, $d) = split ('-', $date);
  173. $unix = mktime (0, 0, 0, $m, $d, $y); */
  174. $unix = Date::toUnix ($date);
  175. if (is_array ($format)) {
  176. if (! empty ($format['today']) && date ('Y-m-d') == date ('Y-m-d', $unix)) {
  177. return localdate ($format['today'], $unix);
  178. } elseif (! empty ($format['yesterday']) && date ('Y-m-d', time () - 86400) == date ('Y-m-d', $unix)) {
  179. return localdate ($format['yesterday'], $unix);
  180. } elseif (! empty ($format['tomorrow']) && date ('Y-m-d', time () + 86400) == date ('Y-m-d', $unix)) {
  181. return localdate ($format['tomorrow'], $unix);
  182. } elseif (! empty ($format['this week']) && date ('Y-W', time ()) == date ('Y-W', $unix)) {
  183. return localdate ($format['this week'], $unix);
  184. } elseif (! empty ($format['other'])) {
  185. return localdate ($format['other'], $unix);
  186. }
  187. } else {
  188. return localdate ($format, $unix);
  189. }
  190. }
  191. /**
  192. * Formats a time provided in ISO format (HH:MM:SS) in the new
  193. * format specified.
  194. *
  195. * @access public
  196. * @param string $date
  197. * @param string $format
  198. * @return string
  199. *
  200. */
  201. function time ($time = '', $format = 'g:ia') {
  202. if (empty ($time)) {
  203. $time = date ('H:i:s');
  204. }
  205. if ($time == '00:00:00') {
  206. return intl_get ('Empty');
  207. }
  208. list ($h, $m, $s) = split (':', $time);
  209. $unix = mktime ($h, $m, $s, date ('m'), date ('d'), date ('Y'));
  210. return date ($format, $unix);
  211. }
  212. /**
  213. * Formats a timestamp provided in ISO format (YYYY-MM-DD HH:MM:SS) in
  214. * the new format specified.
  215. *
  216. * @access public
  217. * @param string $date
  218. * @param string $format
  219. * @return string
  220. *
  221. */
  222. function timestamp ($timestamp = '', $format = 'M j, Y h:i:s a') {
  223. if (empty ($timestamp)) {
  224. $timestamp = localdate ('YmdHis');
  225. }
  226. if ($timestamp == '00000000000000' || $timestamp == '0000-00-00 00:00:00') {
  227. return intl_get ('Empty');
  228. }
  229. if (is_array ($format)) {
  230. if (ereg ('^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$', $timestamp, $regs)) {
  231. $unix = mktime ($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  232. } elseif (ereg ('^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$', $timestamp, $regs)) {
  233. $unix = mktime ($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  234. } else {
  235. return $timestamp;
  236. }
  237. if (! empty ($format['today']) && date ('Y-m-d') == date ('Y-m-d', $unix)) {
  238. return localdate ($format['today'], $unix);
  239. } elseif (! empty ($format['yesterday']) && date ('Y-m-d', time () - 86400) == date ('Y-m-d', $unix)) {
  240. return localdate ($format['yesterday'], $unix);
  241. } elseif (! empty ($format['tomorrow']) && date ('Y-m-d', time () + 86400) == date ('Y-m-d', $unix)) {
  242. return localdate ($format['tomorrow'], $unix);
  243. } elseif (! empty ($format['this week']) && date ('Y-W', time ()) == date ('Y-W', $unix)) {
  244. return localdate ($format['this week'], $unix);
  245. } elseif (! empty ($format['other'])) {
  246. return localdate ($format['other'], $unix);
  247. }
  248. } else {
  249. if (ereg ('^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$', $timestamp, $regs)) {
  250. return localdate ($format, mktime ($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]));
  251. } elseif (ereg ('^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$', $timestamp, $regs)) {
  252. return localdate ($format, mktime ($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]));
  253. } else {
  254. return $timestamp;
  255. }
  256. }
  257. }
  258. /**
  259. * Adds to the specified date and returns the finished calculation.
  260. * $date is in the format Y-m-d, and $amount can be either '# year',
  261. * '# month', '# week', or '# day', where # is any number.
  262. *
  263. * @access public
  264. * @param string $date
  265. * @param string $amount
  266. * @return string
  267. *
  268. */
  269. function add ($date, $amount) {
  270. //$stamp = Date::format ($date, 'U');
  271. if (strpos ($date, ' ') === false) {
  272. $date .= ' 05:05:05';
  273. }
  274. $stamp = Date::toUnix ($date);
  275. $amounts = array (
  276. 'year' => 31536000,
  277. 'month' => 0,
  278. 'week' => 604800,
  279. 'day' => 86400,
  280. // 'hour' => 3600,
  281. // 'minute' => 60,
  282. // 'second' => 1,
  283. );
  284. if (preg_match ('/^([0-9]+) ?(' . join ('|', array_keys ($amounts)) . ')s?$/', $amount, $regs)) {
  285. if ($regs[2] == 'month') {
  286. $hour = date ('H', $stamp);
  287. $minute = date ('i', $stamp);
  288. $second = date ('s', $stamp);
  289. $year = date ('Y', $stamp);
  290. $month = date ('m', $stamp);
  291. $day = date ('d', $stamp);
  292. $month += $regs[1];
  293. //$month++;
  294. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  295. return date ('Y-m-d', $stamp);
  296. } elseif ($regs[2] == 'year') {
  297. $hour = date ('H', $stamp);
  298. $minute = date ('i', $stamp);
  299. $second = date ('s', $stamp);
  300. $year = date ('Y', $stamp);
  301. $month = date ('m', $stamp);
  302. $day = date ('d', $stamp);
  303. $year += $regs[1];
  304. //$year++;
  305. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  306. return date ('Y-m-d', $stamp);
  307. } else {
  308. return date ('Y-m-d', $stamp + ($regs[1] * $amounts[$regs[2]]));
  309. }
  310. } else {
  311. return false;
  312. }
  313. }
  314. /**
  315. * Subtracts from the specified date and returns the finished
  316. * calculation. $date is in the format Y-m-d, and $amount can be either
  317. * '# year', '# month', '# week', or '# day', where # is any number.
  318. *
  319. * @access public
  320. * @param string $date
  321. * @param string $amount
  322. * @return string
  323. *
  324. */
  325. function subtract ($date, $amount) {
  326. //$stamp = Date::format ($date, 'U');
  327. if (strpos ($date, ' ') === false) {
  328. $date .= ' 05:05:05';
  329. }
  330. $stamp = Date::toUnix ($date);
  331. $amounts = array (
  332. 'year' => 31536000,
  333. 'month' => 0,
  334. 'week' => 604800,
  335. 'day' => 86400,
  336. // 'hour' => 3600,
  337. // 'minute' => 60,
  338. // 'second' => 1,
  339. );
  340. if (preg_match ('/^([0-9]+) ?(' . join ('|', array_keys ($amounts)) . ')s?$/', $amount, $regs)) {
  341. if ($regs[2] == 'month') {
  342. $hour = date ('H', $stamp);
  343. $minute = date ('i', $stamp);
  344. $second = date ('s', $stamp);
  345. $year = date ('Y', $stamp);
  346. $month = date ('m', $stamp);
  347. $day = date ('d', $stamp);
  348. $month -= $regs[1];
  349. //$month--;
  350. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  351. return date ('Y-m-d', $stamp);
  352. } elseif ($regs[2] == 'year') {
  353. $hour = date ('H', $stamp);
  354. $minute = date ('i', $stamp);
  355. $second = date ('s', $stamp);
  356. $year = date ('Y', $stamp);
  357. $month = date ('m', $stamp);
  358. $day = date ('d', $stamp);
  359. $year -= $regs[1];
  360. //$year--;
  361. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  362. return date ('Y-m-d', $stamp);
  363. } else {
  364. return date ('Y-m-d', $stamp - ($regs[1] * $amounts[$regs[2]]));
  365. }
  366. } else {
  367. return false;
  368. }
  369. }
  370. /**
  371. * Parses a time string (format: HH:MM:SS) and rounds it to
  372. * the nearest 15 minutes, 1/2 hour, or hour, depending on the interval
  373. * value set. $interval may be 15, 30, or 60. Returns the time as
  374. * a string in the same format as it accepts.
  375. *
  376. * @access public
  377. * @param string $time
  378. * @param integer $interval
  379. * @return string
  380. *
  381. */
  382. function roundTime ($time, $interval = 15) {
  383. list ($hour, $min, $sec) = split (':', $time);
  384. if ($interval == 15) {
  385. $sec = '00';
  386. if ($min <= 7) {
  387. $min = '00';
  388. } elseif ($min <= 22) {
  389. $min = '15';
  390. } elseif ($min <= 37) {
  391. $min = '30';
  392. } elseif ($min <= 52) {
  393. $min = '45';
  394. } else {
  395. $min = '00';
  396. $hour++;
  397. }
  398. if ($hour == 24) {
  399. $hour = '00';
  400. }
  401. } elseif ($interval == 30) {
  402. $sec = '00';
  403. if ($min < 15) {
  404. $min = '00';
  405. } elseif ($min < 45) {
  406. $min = '30';
  407. } else {
  408. $min = '00';
  409. $hour++;
  410. }
  411. if ($hour == 24) {
  412. $hour = '00';
  413. }
  414. } elseif ($interval == 60) {
  415. $sec = '00';
  416. if ($min < 30) {
  417. $min = '00';
  418. } else {
  419. $min = '00';
  420. $hour++;
  421. }
  422. if ($hour == 24) {
  423. $hour = '00';
  424. }
  425. }
  426. return $hour . ':' . $min . ':' . $sec;
  427. }
  428. function convert ($frmt) {
  429. $new = '';
  430. $esc = false;
  431. $lookup = array (
  432. 'a' => '%p', // AM or PM
  433. 'A' => '%p', // AM or PM
  434. 'B' => '',
  435. 'c' => '',
  436. 'd' => '%d', // Day of month 01 - 31
  437. 'D' => '%a', // Mon - Sun
  438. 'F' => '%B', // January - December
  439. 'g' => '%I', // Hour 01 - 12
  440. 'G' => '%H', // Hour 01 - 23
  441. 'h' => '%I', // Hour 01 - 12
  442. 'H' => '%H', // Hour 01 - 23
  443. 'i' => '', // Second 00 - 59
  444. 'I' => '',
  445. 'j' => '%d', // Day of month 01 - 31
  446. 'l' => '%A', // Monday - Sunday
  447. 'L' => '',
  448. 'm' => '%m', // Month 01 - 12
  449. 'M' => '%b', // Jan - Dec
  450. 'n' => '%m', // Month 01 - 12
  451. 'O' => '', // +0200
  452. 'r' => '',
  453. 's' => '%S', // Second 00 - 59
  454. 'S' => '', // st, nd, rd, th
  455. 't' => '', // 28 - 31
  456. 'T' => '%Z', // EST, MDT
  457. 'U' => '',
  458. 'w' => '%w', // Day of week 0 - 6
  459. 'W' => '',
  460. 'y' => '%y', // Year 04
  461. 'Y' => '%Y', // Year 2004
  462. 'z' => '',
  463. 'Z' => '',
  464. );
  465. }
  466. }
  467. ?>