phprpc_date.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | The implementation of PHPRPC Protocol 3.0 |
  5. | |
  6. | phprpc_date.php |
  7. | |
  8. | Release 3.0.1 |
  9. | Copyright by Team-PHPRPC |
  10. | |
  11. | WebSite: http://www.phprpc.org/ |
  12. | http://www.phprpc.net/ |
  13. | http://www.phprpc.com/ |
  14. | http://sourceforge.net/projects/php-rpc/ |
  15. | |
  16. | Authors: Ma Bingyao <andot@ujn.edu.cn> |
  17. | |
  18. | This file may be distributed and/or modified under the |
  19. | terms of the GNU General Public License (GPL) version |
  20. | 2.0 as published by the Free Software Foundation and |
  21. | appearing in the included file LICENSE. |
  22. | |
  23. \**********************************************************/
  24. /* PHPRPC_Date Class for PHP.
  25. *
  26. * Copyright: Ma Bingyao <andot@ujn.edu.cn>
  27. * Version: 1.2
  28. * LastModified: Apr 12, 2010
  29. * This library is free. You can redistribute it and/or modify it under GPL.
  30. */
  31. class PHPRPC_Date {
  32. // public fields
  33. var $year = 1;
  34. var $month = 1;
  35. var $day = 1;
  36. var $hour = 0;
  37. var $minute = 0;
  38. var $second = 0;
  39. var $millisecond = 0;
  40. // constructor
  41. function PHPRPC_Date() {
  42. $num = func_num_args();
  43. $time = false;
  44. if ($num == 0) {
  45. $time = getdate();
  46. }
  47. if ($num == 1) {
  48. $arg = func_get_arg(0);
  49. if (is_int($arg)) {
  50. $time = getdate($arg);
  51. }
  52. elseif (is_string($arg)) {
  53. $time = getdate(strtotime($arg));
  54. }
  55. }
  56. if (is_array($time)) {
  57. $this->year = $time['year'];
  58. $this->month = $time['mon'];
  59. $this->day = $time['mday'];
  60. $this->hour = $time['hours'];
  61. $this->minute = $time['minutes'];
  62. $this->second = $time['seconds'];
  63. }
  64. }
  65. // public instance methods
  66. function addMilliseconds($milliseconds) {
  67. if (!is_int($milliseconds)) return false;
  68. if ($milliseconds == 0) return true;
  69. $millisecond = $this->millisecond + $milliseconds;
  70. $milliseconds = $millisecond % 1000;
  71. if ($milliseconds < 0) {
  72. $milliseconds += 1000;
  73. }
  74. $seconds = (int)(($millisecond - $milliseconds) / 1000);
  75. $millisecond = (int)$milliseconds;
  76. if ($this->addSeconds($seconds)) {
  77. $this->millisecond = (int)$milliseconds;
  78. return true;
  79. }
  80. else {
  81. return false;
  82. }
  83. }
  84. function addSeconds($seconds) {
  85. if (!is_int($seconds)) return false;
  86. if ($seconds == 0) return true;
  87. $second = $this->second + $seconds;
  88. $seconds = $second % 60;
  89. if ($seconds < 0) {
  90. $seconds += 60;
  91. }
  92. $minutes = (int)(($second - $seconds) / 60);
  93. if ($this->addMinutes($minutes)) {
  94. $this->second = (int)$seconds;
  95. return true;
  96. }
  97. else {
  98. return false;
  99. }
  100. }
  101. function addMinutes($minutes) {
  102. if (!is_int($minutes)) return false;
  103. if ($minutes == 0) return true;
  104. $minute = $this->minute + $minutes;
  105. $minutes = $minute % 60;
  106. if ($minutes < 0) {
  107. $minutes += 60;
  108. }
  109. $hours = (int)(($minute - $minutes) / 60);
  110. if ($this->addHours($hours)) {
  111. $this->minute = (int)$minutes;
  112. return true;
  113. }
  114. else {
  115. return false;
  116. }
  117. }
  118. function addHours($hours) {
  119. if (!is_int($hours)) return false;
  120. if ($hours == 0) return true;
  121. $hour = $this->hour + $hours;
  122. $hours = $hour % 24;
  123. if ($hours < 0) {
  124. $hours += 24;
  125. }
  126. $days = (int)(($hour - $hours) / 24);
  127. if ($this->addDays($days)) {
  128. $this->hour = (int)$hours;
  129. return true;
  130. }
  131. else {
  132. return false;
  133. }
  134. }
  135. function addDays($days) {
  136. if (!is_int($days)) return false;
  137. $year = $this->year;
  138. if ($days == 0) return true;
  139. if ($days >= 146097 || $days <= -146097) {
  140. $remainder = $days % 146097;
  141. if ($remainder < 0) {
  142. $remainder += 146097;
  143. }
  144. $years = 400 * (int)(($days - $remainder) / 146097);
  145. $year += $years;
  146. if ($year < 1 || $year > 9999) return false;
  147. $days = $remainder;
  148. }
  149. if ($days >= 36524 || $days <= -36524) {
  150. $remainder = $days % 36524;
  151. if ($remainder < 0) {
  152. $remainder += 36524;
  153. }
  154. $years = 100 * (int)(($days - $remainder) / 36524);
  155. $year += $years;
  156. if ($year < 1 || $year > 9999) return false;
  157. $days = $remainder;
  158. }
  159. if ($days >= 1461 || $days <= -1461) {
  160. $remainder = $days % 1461;
  161. if ($remainder < 0) {
  162. $remainder += 1461;
  163. }
  164. $years = 4 * (int)(($days - $remainder) / 1461);
  165. $year += $years;
  166. if ($year < 1 || $year > 9999) return false;
  167. $days = $remainder;
  168. }
  169. $month = $this->month;
  170. while ($days >= 365) {
  171. if ($year >= 9999) return false;
  172. if ($month <= 2) {
  173. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  174. $days -= 366;
  175. }
  176. else {
  177. $days -= 365;
  178. }
  179. $year++;
  180. }
  181. else {
  182. $year++;
  183. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  184. $days -= 366;
  185. }
  186. else {
  187. $days -= 365;
  188. }
  189. }
  190. }
  191. while ($days < 0) {
  192. if ($year <= 1) return false;
  193. if ($month <= 2) {
  194. $year--;
  195. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  196. $days += 366;
  197. }
  198. else {
  199. $days += 365;
  200. }
  201. }
  202. else {
  203. if ((($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false) {
  204. $days += 366;
  205. }
  206. else {
  207. $days += 365;
  208. }
  209. $year--;
  210. }
  211. }
  212. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  213. $day = $this->day;
  214. while ($day + $days > $daysInMonth) {
  215. $days -= $daysInMonth - $day + 1;
  216. $month++;
  217. if ($month > 12) {
  218. if ($year >= 9999) return false;
  219. $year++;
  220. $month = 1;
  221. }
  222. $day = 1;
  223. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  224. }
  225. $day += $days;
  226. $this->year = $year;
  227. $this->month = $month;
  228. $this->day = $day;
  229. return true;
  230. }
  231. function addMonths($months) {
  232. if (!is_int($months)) return false;
  233. if ($months == 0) return true;
  234. $month = $this->month + $months;
  235. $months = ($month - 1) % 12 + 1;
  236. if ($months < 1) {
  237. $months += 12;
  238. }
  239. $years = (int)(($month - $months) / 12);
  240. if ($this->addYears($years)) {
  241. $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $months, $this->year);
  242. if ($this->day > $daysInMonth) {
  243. $months++;
  244. $this->day -= $daysInMonth;
  245. }
  246. $this->month = (int)$months;
  247. return true;
  248. }
  249. else {
  250. return false;
  251. }
  252. }
  253. function addYears($years) {
  254. if (!is_int($years)) return false;
  255. if ($years == 0) return true;
  256. $year = $this->year + $years;
  257. if ($year < 1 || $year > 9999) return false;
  258. $this->year = $year;
  259. return true;
  260. }
  261. function after($when) {
  262. if (!is_a($when, 'PHPRPC_Date')) {
  263. $when = PHPRPC_Date::parse($when);
  264. }
  265. if ($this->year < $when->year) return false;
  266. if ($this->year > $when->year) return true;
  267. if ($this->month < $when->month) return false;
  268. if ($this->month > $when->month) return true;
  269. if ($this->day < $when->day) return false;
  270. if ($this->day > $when->day) return true;
  271. if ($this->hour < $when->hour) return false;
  272. if ($this->hour > $when->hour) return true;
  273. if ($this->minute < $when->minute) return false;
  274. if ($this->minute > $when->minute) return true;
  275. if ($this->second < $when->second) return false;
  276. if ($this->second > $when->second) return true;
  277. if ($this->millisecond < $when->millisecond) return false;
  278. if ($this->millisecond > $when->millisecond) return true;
  279. return false;
  280. }
  281. function before($when) {
  282. if (!is_a($when, 'PHPRPC_Date')) {
  283. $when = new PHPRPC_Date($when);
  284. }
  285. if ($this->year < $when->year) return true;
  286. if ($this->year > $when->year) return false;
  287. if ($this->month < $when->month) return true;
  288. if ($this->month > $when->month) return false;
  289. if ($this->day < $when->day) return true;
  290. if ($this->day > $when->day) return false;
  291. if ($this->hour < $when->hour) return true;
  292. if ($this->hour > $when->hour) return false;
  293. if ($this->minute < $when->minute) return true;
  294. if ($this->minute > $when->minute) return false;
  295. if ($this->second < $when->second) return true;
  296. if ($this->second > $when->second) return false;
  297. if ($this->millisecond < $when->millisecond) return true;
  298. if ($this->millisecond > $when->millisecond) return false;
  299. return false;
  300. }
  301. function equals($when) {
  302. if (!is_a($when, 'PHPRPC_Date')) {
  303. $when = new PHPRPC_Date($when);
  304. }
  305. return (($this->year == $when->year) &&
  306. ($this->month == $when->month) &&
  307. ($this->day == $when->day) &&
  308. ($this->hour == $when->hour) &&
  309. ($this->minute == $when->minute) &&
  310. ($this->second == $when->second) &&
  311. ($this->millisecond == $when->millisecond));
  312. }
  313. function set() {
  314. $num = func_num_args();
  315. $args = func_get_args();
  316. if ($num >= 3) {
  317. if (!PHPRPC_Date::isValidDate($args[0], $args[1], $args[2])) {
  318. return false;
  319. }
  320. $this->year = (int)$args[0];
  321. $this->month = (int)$args[1];
  322. $this->day = (int)$args[2];
  323. if ($num == 3) {
  324. return true;
  325. }
  326. }
  327. if ($num >= 6) {
  328. if (!PHPRPC_Date::isValidTime($args[3], $args[4], $args[5])) {
  329. return false;
  330. }
  331. $this->hour = (int)$args[3];
  332. $this->minute = (int)$args[4];
  333. $this->second = (int)$args[5];
  334. if ($num == 6) {
  335. return true;
  336. }
  337. }
  338. if (($num == 7) && ($args[6] >= 0 && $args[6] <= 999)) {
  339. $this->millisecond = (int)$args[6];
  340. return true;
  341. }
  342. return false;
  343. }
  344. function time() {
  345. return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
  346. }
  347. function toString() {
  348. return sprintf('%04d-%02d-%02d %02d:%02d:%02d.%03d',
  349. $this->year, $this->month, $this->day,
  350. $this->hour, $this->minute, $this->second,
  351. $this->millisecond);
  352. }
  353. // magic method for PHP 5
  354. function __toString() {
  355. return $this->toString();
  356. }
  357. // public instance & static methods
  358. function dayOfWeek() {
  359. $num = func_num_args();
  360. if ($num == 3) {
  361. $args = func_get_args();
  362. $y = $args[0];
  363. $m = $args[1];
  364. $d = $args[2];
  365. }
  366. else {
  367. $y = $this->year;
  368. $m = $this->month;
  369. $d = $this->day;
  370. }
  371. $d += $m < 3 ? $y-- : $y - 2;
  372. return ((int)(23 * $m / 9) + $d + 4 + (int)($y / 4) - (int)($y / 100) + (int)($y / 400)) % 7;
  373. }
  374. function dayOfYear() {
  375. static $daysToMonth365 = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
  376. static $daysToMonth366 = array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366);
  377. $num = func_num_args();
  378. if ($num == 3) {
  379. $args = func_get_args();
  380. $y = $args[0];
  381. $m = $args[1];
  382. $d = $args[2];
  383. }
  384. else {
  385. $y = $this->year;
  386. $m = $this->month;
  387. $d = $this->day;
  388. }
  389. $days = PHPRPC_Date::isLeapYear($y) ? $daysToMonth365 : $daysToMonth366;
  390. return $days[$m - 1] + $d;
  391. }
  392. // public static methods
  393. function now() {
  394. $date = new PHPRPC_Date();
  395. return $date;
  396. }
  397. function today() {
  398. $date = PHPRPC_Date::now();
  399. $date->hour = 0;
  400. $date->minute = 0;
  401. $date->second = 0;
  402. return $date;
  403. }
  404. function parse($dt) {
  405. if (is_a($dt, 'PHPRPC_Date')) {
  406. return $dt;
  407. }
  408. if (is_int($dt)) {
  409. return new PHPRPC_Date($dt);
  410. }
  411. $shortFormat = '(\d|\d{2}|\d{3}|\d{4})-([1-9]|0[1-9]|1[012])-([1-9]|0[1-9]|[12]\d|3[01])';
  412. if (preg_match("/^$shortFormat$/", $dt, $match)) {
  413. $year = intval($match[1]);
  414. $month = intval($match[2]);
  415. $day = intval($match[3]);
  416. if (PHPRPC_Date::isValidDate($year, $month, $day)) {
  417. $date = new PHPRPC_Date(false);
  418. $date->year = $year;
  419. $date->month = $month;
  420. $date->day = $day;
  421. return $date;
  422. }
  423. else {
  424. return false;
  425. }
  426. }
  427. $longFormat = $shortFormat . ' (\d|0\d|1\d|2[0-3]):(\d|[0-5]\d):(\d|[0-5]\d)';
  428. if (preg_match("/^$longFormat$/", $dt, $match)) {
  429. $year = intval($match[1]);
  430. $month = intval($match[2]);
  431. $day = intval($match[3]);
  432. if (PHPRPC_Date::isValidDate($year, $month, $day)) {
  433. $date = new PHPRPC_Date(false);
  434. $date->year = $year;
  435. $date->month = $month;
  436. $date->day = $day;
  437. $date->hour = intval($match[4]);
  438. $date->minute = intval($match[5]);
  439. $date->second = intval($match[6]);
  440. return $date;
  441. }
  442. else {
  443. return false;
  444. }
  445. }
  446. $fullFormat = $longFormat . '\.(\d|\d{2}|\d{3})';
  447. if (preg_match("/^$fullFormat$/", $dt, $match)) {
  448. $year = intval($match[1]);
  449. $month = intval($match[2]);
  450. $day = intval($match[3]);
  451. if (PHPRPC_Date::isValidDate($year, $month, $day)) {
  452. $date = new PHPRPC_Date(false);
  453. $date->year = $year;
  454. $date->month = $month;
  455. $date->day = $day;
  456. $date->hour = intval($match[4]);
  457. $date->minute = intval($match[5]);
  458. $date->second = intval($match[6]);
  459. $date->millisecond = intval($match[7]);
  460. return $date;
  461. }
  462. else {
  463. return false;
  464. }
  465. }
  466. return false;
  467. }
  468. function isLeapYear($year) {
  469. return (($year % 4) == 0) ? (($year % 100) == 0) ? (($year % 400) == 0) : true : false;
  470. }
  471. function daysInMonth($year, $month) {
  472. if (($month < 1) || ($month > 12)) {
  473. return false;
  474. }
  475. return cal_days_in_month(CAL_GREGORIAN, $month, $year);
  476. }
  477. function isValidDate($year, $month, $day) {
  478. if (($year >= 1) && ($year <= 9999)) {
  479. return checkdate($month, $day, $year);
  480. }
  481. return false;
  482. }
  483. function isValidTime($hour, $minute, $second) {
  484. return !(($hour < 0) || ($hour > 23) ||
  485. ($minute < 0) || ($minute > 59) ||
  486. ($second < 0) || ($second > 59));
  487. }
  488. }
  489. ?>