PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/saf/lib/Date/Date.php

https://github.com/lux/siteforge
PHP | 477 lines | 269 code | 14 blank | 194 comment | 71 complexity | 692e35b114e97eca73078ab940e43894 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
  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++;
  293. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  294. return date ('Y-m-d', $stamp);
  295. } elseif ($regs[2] == 'year') {
  296. $hour = date ('H', $stamp);
  297. $minute = date ('i', $stamp);
  298. $second = date ('s', $stamp);
  299. $year = date ('Y', $stamp);
  300. $month = date ('m', $stamp);
  301. $day = date ('d', $stamp);
  302. $year++;
  303. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  304. return date ('Y-m-d', $stamp);
  305. } else {
  306. return date ('Y-m-d', $stamp + ($regs[1] * $amounts[$regs[2]]));
  307. }
  308. } else {
  309. return false;
  310. }
  311. }
  312. /**
  313. * Subtracts from the specified date and returns the finished
  314. * calculation. $date is in the format Y-m-d, and $amount can be either
  315. * '# year', '# month', '# week', or '# day', where # is any number.
  316. *
  317. * @access public
  318. * @param string $date
  319. * @param string $amount
  320. * @return string
  321. *
  322. */
  323. function subtract ($date, $amount) {
  324. //$stamp = Date::format ($date, 'U');
  325. if (strpos ($date, ' ') === false) {
  326. $date .= ' 05:05:05';
  327. }
  328. $stamp = Date::toUnix ($date);
  329. $amounts = array (
  330. 'year' => 31536000,
  331. 'month' => 0,
  332. 'week' => 604800,
  333. 'day' => 86400,
  334. // 'hour' => 3600,
  335. // 'minute' => 60,
  336. // 'second' => 1,
  337. );
  338. if (preg_match ('/^([0-9]+) ?(' . join ('|', array_keys ($amounts)) . ')s?$/', $amount, $regs)) {
  339. if ($regs[2] == 'month') {
  340. $hour = date ('H', $stamp);
  341. $minute = date ('i', $stamp);
  342. $second = date ('s', $stamp);
  343. $year = date ('Y', $stamp);
  344. $month = date ('m', $stamp);
  345. $day = date ('d', $stamp);
  346. $month--;
  347. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  348. return date ('Y-m-d', $stamp);
  349. } elseif ($regs[2] == 'year') {
  350. $hour = date ('H', $stamp);
  351. $minute = date ('i', $stamp);
  352. $second = date ('s', $stamp);
  353. $year = date ('Y', $stamp);
  354. $month = date ('m', $stamp);
  355. $day = date ('d', $stamp);
  356. $year--;
  357. $stamp = mktime ($hour, $minute, $second, $month, $day, $year);
  358. return date ('Y-m-d', $stamp);
  359. } else {
  360. return date ('Y-m-d', $stamp - ($regs[1] * $amounts[$regs[2]]));
  361. }
  362. } else {
  363. return false;
  364. }
  365. }
  366. /**
  367. * Parses a time string (format: HH:MM:SS) and rounds it to
  368. * the nearest 15 minutes, 1/2 hour, or hour, depending on the interval
  369. * value set. $interval may be 15, 30, or 60. Returns the time as
  370. * a string in the same format as it accepts.
  371. *
  372. * @access public
  373. * @param string $time
  374. * @param integer $interval
  375. * @return string
  376. *
  377. */
  378. function roundTime ($time, $interval = 15) {
  379. list ($hour, $min, $sec) = split (':', $time);
  380. if ($interval == 15) {
  381. $sec = '00';
  382. if ($min <= 7) {
  383. $min = '00';
  384. } elseif ($min <= 22) {
  385. $min = '15';
  386. } elseif ($min <= 37) {
  387. $min = '30';
  388. } elseif ($min <= 52) {
  389. $min = '45';
  390. } else {
  391. $min = '00';
  392. $hour++;
  393. }
  394. if ($hour == 24) {
  395. $hour = '00';
  396. }
  397. } elseif ($interval == 30) {
  398. $sec = '00';
  399. if ($min < 15) {
  400. $min = '00';
  401. } elseif ($min < 45) {
  402. $min = '30';
  403. } else {
  404. $min = '00';
  405. $hour++;
  406. }
  407. if ($hour == 24) {
  408. $hour = '00';
  409. }
  410. } elseif ($interval == 60) {
  411. $sec = '00';
  412. if ($min < 30) {
  413. $min = '00';
  414. } else {
  415. $min = '00';
  416. $hour++;
  417. }
  418. if ($hour == 24) {
  419. $hour = '00';
  420. }
  421. }
  422. return $hour . ':' . $min . ':' . $sec;
  423. }
  424. function convert ($frmt) {
  425. $new = '';
  426. $esc = false;
  427. $lookup = array (
  428. 'a' => '%p', // AM or PM
  429. 'A' => '%p', // AM or PM
  430. 'B' => '',
  431. 'c' => '',
  432. 'd' => '%d', // Day of month 01 - 31
  433. 'D' => '%a', // Mon - Sun
  434. 'F' => '%B', // January - December
  435. 'g' => '%I', // Hour 01 - 12
  436. 'G' => '%H', // Hour 01 - 23
  437. 'h' => '%I', // Hour 01 - 12
  438. 'H' => '%H', // Hour 01 - 23
  439. 'i' => '', // Second 00 - 59
  440. 'I' => '',
  441. 'j' => '%d', // Day of month 01 - 31
  442. 'l' => '%A', // Monday - Sunday
  443. 'L' => '',
  444. 'm' => '%m', // Month 01 - 12
  445. 'M' => '%b', // Jan - Dec
  446. 'n' => '%m', // Month 01 - 12
  447. 'O' => '', // +0200
  448. 'r' => '',
  449. 's' => '%S', // Second 00 - 59
  450. 'S' => '', // st, nd, rd, th
  451. 't' => '', // 28 - 31
  452. 'T' => '%Z', // EST, MDT
  453. 'U' => '',
  454. 'w' => '%w', // Day of week 0 - 6
  455. 'W' => '',
  456. 'y' => '%y', // Year 04
  457. 'Y' => '%Y', // Year 2004
  458. 'z' => '',
  459. 'Z' => '',
  460. );
  461. }
  462. }
  463. ?>