Calc.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. <?php
  2. // The constant telling us what day starts the week. Monday (1) is the
  3. // international standard. Redefine this to 0 if you want weeks to
  4. // begin on Sunday.
  5. if (!defined('DATE_CALC_BEGIN_WEEKDAY')) {
  6. define('DATE_CALC_BEGIN_WEEKDAY', 1);
  7. }
  8. /**
  9. * Date_Calc is a calendar class used to calculate and
  10. * manipulate calendar dates and retrieve dates in a calendar
  11. * format. It does not rely on 32-bit system date stamps, so
  12. * you can display calendars and compare dates that date
  13. * pre 1970 and post 2038.
  14. *
  15. * This source file is subject to version 2.02 of the PHP license,
  16. * that is bundled with this package in the file LICENSE, and is
  17. * available at through the world-wide-web at
  18. * http://www.php.net/license/2_02.txt.
  19. * If you did not receive a copy of the PHP license and are unable to
  20. * obtain it through the world-wide-web, please send a note to
  21. * license@php.net so we can mail you a copy immediately.
  22. *
  23. * Copyright (c) 1999, 2002, 2003 ispi
  24. *
  25. * @access public
  26. *
  27. * @version 1.2.6
  28. * @author Monte Ohrt <monte@ispi.net>
  29. */
  30. class Date_Calc
  31. {
  32. /**
  33. * Returns the current local date. NOTE: This function
  34. * retrieves the local date using strftime(), which may
  35. * or may not be 32-bit safe on your system.
  36. *
  37. * @param string the strftime() format to return the date
  38. *
  39. * @access public
  40. *
  41. * @return string the current date in specified format
  42. */
  43. function dateNow($format='%Y%m%d')
  44. {
  45. return(strftime($format,time()));
  46. } // end func dateNow
  47. /**
  48. * Returns true for valid date, false for invalid date.
  49. *
  50. * @param string year in format CCYY
  51. * @param string month in format MM
  52. * @param string day in format DD
  53. *
  54. * @access public
  55. *
  56. * @return boolean true/false
  57. */
  58. function isValidDate($day, $month, $year)
  59. {
  60. if ($year < 0 || $year > 9999) {
  61. return false;
  62. }
  63. if (!checkdate($month,$day,$year)) {
  64. return false;
  65. }
  66. return true;
  67. } // end func isValidDate
  68. /**
  69. * Returns true for a leap year, else false
  70. *
  71. * @param string year in format CCYY
  72. *
  73. * @access public
  74. *
  75. * @return boolean true/false
  76. */
  77. function isLeapYear($year='')
  78. {
  79. if (empty($year)) {
  80. $year = Date_Calc::dateNow('%Y');
  81. }
  82. if (preg_match('/\D/',$year)) {
  83. return false;
  84. }
  85. if ($year < 1000) {
  86. return false;
  87. }
  88. if ($year < 1582) {
  89. // pre Gregorio XIII - 1582
  90. return ($year % 4 == 0);
  91. } else {
  92. // post Gregorio XIII - 1582
  93. return ( (($year % 4 == 0) and ($year % 100 != 0)) or ($year % 400 == 0) );
  94. }
  95. } // end func isLeapYear
  96. /**
  97. * Determines if given date is a future date from now.
  98. *
  99. * @param string day in format DD
  100. * @param string month in format MM
  101. * @param string year in format CCYY
  102. *
  103. * @access public
  104. *
  105. * @return boolean true/false
  106. */
  107. function isFutureDate($day,$month,$year)
  108. {
  109. $this_year = Date_Calc::dateNow('%Y');
  110. $this_month = Date_Calc::dateNow('%m');
  111. $this_day = Date_Calc::dateNow('%d');
  112. if ($year > $this_year) {
  113. return true;
  114. } elseif ($year == $this_year) {
  115. if ($month > $this_month) {
  116. return true;
  117. } elseif ($month == $this_month) {
  118. if ($day > $this_day) {
  119. return true;
  120. }
  121. }
  122. }
  123. return false;
  124. } // end func isFutureDate
  125. /**
  126. * Determines if given date is a past date from now.
  127. *
  128. * @param string day in format DD
  129. * @param string month in format MM
  130. * @param string year in format CCYY
  131. *
  132. * @access public
  133. *
  134. * @return boolean true/false
  135. */
  136. function isPastDate($day,$month,$year)
  137. {
  138. $this_year = Date_Calc::dateNow('%Y');
  139. $this_month = Date_Calc::dateNow('%m');
  140. $this_day = Date_Calc::dateNow('%d');
  141. if ($year < $this_year) {
  142. return true;
  143. } elseif ($year == $this_year) {
  144. if ($month < $this_month) {
  145. return true;
  146. } elseif ($month == $this_month) {
  147. if ($day < $this_day) {
  148. return true;
  149. }
  150. }
  151. }
  152. return false;
  153. } // end func isPastDate
  154. /**
  155. * Returns day of week for given date, 0=Sunday
  156. *
  157. * @param string day in format DD, default is current local day
  158. * @param string month in format MM, default is current local month
  159. * @param string year in format CCYY, default is current local year
  160. *
  161. * @access public
  162. *
  163. * @return int $weekday_number
  164. */
  165. function dayOfWeek($day='',$month='',$year='')
  166. {
  167. if (empty($year)) {
  168. $year = Date_Calc::dateNow('%Y');
  169. }
  170. if (empty($month)) {
  171. $month = Date_Calc::dateNow('%m');
  172. }
  173. if (empty($day)) {
  174. $day = Date_Calc::dateNow('%d');
  175. }
  176. if ($month > 2) {
  177. $month -= 2;
  178. } else {
  179. $month += 10;
  180. $year--;
  181. }
  182. $day = ( floor((13 * $month - 1) / 5) +
  183. $day + ($year % 100) +
  184. floor(($year % 100) / 4) +
  185. floor(($year / 100) / 4) - 2 *
  186. floor($year / 100) + 77);
  187. $weekday_number = (($day - 7 * floor($day / 7)));
  188. return $weekday_number;
  189. } // end func dayOfWeek
  190. /**
  191. * Returns week of the year, first Sunday is first day of first week
  192. *
  193. * @param string day in format DD, default is current local day
  194. * @param string month in format MM, default is current local month
  195. * @param string year in format CCYY, default is current local year
  196. *
  197. * @access public
  198. *
  199. * @return integer $week_number
  200. */
  201. function weekOfYear($day='',$month='',$year='')
  202. {
  203. if (empty($year)) {
  204. $year = Date_Calc::dateNow('%Y');
  205. }
  206. if (empty($month)) {
  207. $month = Date_Calc::dateNow('%m');
  208. }
  209. if (empty($day)) {
  210. $day = Date_Calc::dateNow('%d');
  211. }
  212. $iso = Date_Calc::gregorianToISO($day, $month, $year);
  213. $parts = explode('-',$iso);
  214. $week_number = intval($parts[1]);
  215. return $week_number;
  216. } // end func weekOfYear
  217. /**
  218. * Returns number of days since 31 December of year before given date.
  219. *
  220. * @param string day in format DD, default is current local day
  221. * @param string month in format MM, default is current local month
  222. * @param string year in format CCYY, default is current local year
  223. *
  224. * @access public
  225. *
  226. * @return int $julian
  227. */
  228. function julianDate($day='',$month='',$year='')
  229. {
  230. if (empty($year)) {
  231. $year = Date_Calc::dateNow('%Y');
  232. }
  233. if (empty($month)) {
  234. $month = Date_Calc::dateNow('%m');
  235. }
  236. if (empty($day)) {
  237. $day = Date_Calc::dateNow('%d');
  238. }
  239. $days = array(0,31,59,90,120,151,181,212,243,273,304,334);
  240. $julian = ($days[$month - 1] + $day);
  241. if ($month > 2 && Date_Calc::isLeapYear($year)) {
  242. $julian++;
  243. }
  244. return($julian);
  245. } // end func julianDate
  246. /**
  247. * Returns quarter of the year for given date
  248. *
  249. * @param string day in format DD, default is current local day
  250. * @param string month in format MM, default is current local month
  251. * @param string year in format CCYY, default is current local year
  252. *
  253. * @access public
  254. *
  255. * @return int $year_quarter
  256. */
  257. function quarterOfYear($day='',$month='',$year='')
  258. {
  259. if (empty($year)) {
  260. $year = Date_Calc::dateNow('%Y');
  261. }
  262. if (empty($month)) {
  263. $month = Date_Calc::dateNow('%m');
  264. }
  265. if (empty($day)) {
  266. $day = Date_Calc::dateNow('%d');
  267. }
  268. $year_quarter = (intval(($month - 1) / 3 + 1));
  269. return $year_quarter;
  270. } // end func quarterOfYear
  271. /**
  272. * Returns date of begin of next month of given date.
  273. *
  274. * @param string day in format DD, default is current local day
  275. * @param string month in format MM, default is current local month
  276. * @param string year in format CCYY, default is current local year
  277. * @param string format for returned date
  278. *
  279. * @access public
  280. *
  281. * @return string date in given format
  282. */
  283. function beginOfNextMonth($day='',$month='',$year='',$format='%Y%m%d')
  284. {
  285. if (empty($year)) {
  286. $year = Date_Calc::dateNow('%Y');
  287. }
  288. if (empty($month)) {
  289. $month = Date_Calc::dateNow('%m');
  290. }
  291. if (empty($day)) {
  292. $day = Date_Calc::dateNow('%d');
  293. }
  294. if ($month < 12) {
  295. $month++;
  296. $day=1;
  297. } else {
  298. $year++;
  299. $month=1;
  300. $day=1;
  301. }
  302. return Date_Calc::dateFormat($day,$month,$year,$format);
  303. } // end func beginOfNextMonth
  304. /**
  305. * Returns date of the last day of next month of given date.
  306. *
  307. * @param string day in format DD, default is current local day
  308. * @param string month in format MM, default is current local month
  309. * @param string year in format CCYY, default is current local year
  310. * @param string format for returned date
  311. *
  312. * @access public
  313. *
  314. * @return string date in given format
  315. */
  316. function endOfNextMonth($day='',$month='',$year='',$format='%Y%m%d')
  317. {
  318. if (empty($year)) {
  319. $year = Date_Calc::dateNow('%Y');
  320. }
  321. if (empty($month)) {
  322. $month = Date_Calc::dateNow('%m');
  323. }
  324. if (empty($day)) {
  325. $day = Date_Calc::dateNow('%d');
  326. }
  327. if ($month < 12) {
  328. $month++;
  329. } else {
  330. $year++;
  331. $month=1;
  332. }
  333. $day = Date_Calc::daysInMonth($month,$year);
  334. return Date_Calc::dateFormat($day,$month,$year,$format);
  335. } // end func endOfNextMonth
  336. /**
  337. * Returns date of the first day of previous month of given date.
  338. *
  339. * @param string day in format DD, default is current local day
  340. * @param string month in format MM, default is current local month
  341. * @param string year in format CCYY, default is current local year
  342. * @param string format for returned date
  343. *
  344. * @access public
  345. *
  346. * @return string date in given format
  347. */
  348. function beginOfPrevMonth($day='',$month='',$year='',$format='%Y%m%d')
  349. {
  350. if (empty($year)) {
  351. $year = Date_Calc::dateNow('%Y');
  352. }
  353. if (empty($month)) {
  354. $month = Date_Calc::dateNow('%m');
  355. }
  356. if (empty($day)) {
  357. $day = Date_Calc::dateNow('%d');
  358. }
  359. if ($month > 1) {
  360. $month--;
  361. $day=1;
  362. } else {
  363. $year--;
  364. $month=12;
  365. $day=1;
  366. }
  367. return Date_Calc::dateFormat($day,$month,$year,$format);
  368. } // end func beginOfPrevMonth
  369. /**
  370. * Returns date of the last day of previous month for given date.
  371. *
  372. * @param string day in format DD, default is current local day
  373. * @param string month in format MM, default is current local month
  374. * @param string year in format CCYY, default is current local year
  375. * @param string format for returned date
  376. *
  377. * @access public
  378. *
  379. * @return string date in given format
  380. */
  381. function endOfPrevMonth($day='',$month='',$year='',$format='%Y%m%d')
  382. {
  383. if (empty($year)) {
  384. $year = Date_Calc::dateNow('%Y');
  385. }
  386. if (empty($month)) {
  387. $month = Date_Calc::dateNow('%m');
  388. }
  389. if (empty($day)) {
  390. $day = Date_Calc::dateNow('%d');
  391. }
  392. if ($month > 1) {
  393. $month--;
  394. } else {
  395. $year--;
  396. $month=12;
  397. }
  398. $day = Date_Calc::daysInMonth($month,$year);
  399. return Date_Calc::dateFormat($day,$month,$year,$format);
  400. } // end func endOfPrevMonth
  401. /**
  402. * Returns date of the next weekday of given date,
  403. * skipping from Friday to Monday.
  404. *
  405. * @param string day in format DD, default is current local day
  406. * @param string month in format MM, default is current local month
  407. * @param string year in format CCYY, default is current local year
  408. * @param string format for returned date
  409. *
  410. * @access public
  411. *
  412. * @return string date in given format
  413. */
  414. function nextWeekday($day='',$month='',$year='',$format='%Y%m%d')
  415. {
  416. if (empty($year)) {
  417. $year = Date_Calc::dateNow('%Y');
  418. }
  419. if (empty($month)) {
  420. $month = Date_Calc::dateNow('%m');
  421. }
  422. if (empty($day)) {
  423. $day = Date_Calc::dateNow('%d');
  424. }
  425. $days = Date_Calc::dateToDays($day,$month,$year);
  426. if (Date_Calc::dayOfWeek($day,$month,$year) == 5) {
  427. $days += 3;
  428. } elseif (Date_Calc::dayOfWeek($day,$month,$year) == 6) {
  429. $days += 2;
  430. } else {
  431. $days += 1;
  432. }
  433. return(Date_Calc::daysToDate($days,$format));
  434. } // end func nextWeekday
  435. /**
  436. * Returns date of the previous weekday,
  437. * skipping from Monday to Friday.
  438. *
  439. * @param string day in format DD, default is current local day
  440. * @param string month in format MM, default is current local month
  441. * @param string year in format CCYY, default is current local year
  442. * @param string format for returned date
  443. *
  444. * @access public
  445. *
  446. * @return string date in given format
  447. */
  448. function prevWeekday($day='',$month='',$year='',$format='%Y%m%d')
  449. {
  450. if (empty($year)) {
  451. $year = Date_Calc::dateNow('%Y');
  452. }
  453. if (empty($month)) {
  454. $month = Date_Calc::dateNow('%m');
  455. }
  456. if (empty($day)) {
  457. $day = Date_Calc::dateNow('%d');
  458. }
  459. $days = Date_Calc::dateToDays($day,$month,$year);
  460. if (Date_Calc::dayOfWeek($day,$month,$year) == 1) {
  461. $days -= 3;
  462. } elseif (Date_Calc::dayOfWeek($day,$month,$year) == 0) {
  463. $days -= 2;
  464. } else {
  465. $days -= 1;
  466. }
  467. return(Date_Calc::daysToDate($days,$format));
  468. } // end func prevWeekday
  469. /**
  470. * Returns date of the next specific day of the week
  471. * from the given date.
  472. *
  473. * @param int day of week, 0=Sunday
  474. * @param string day in format DD, default is current local day
  475. * @param string month in format MM, default is current local month
  476. * @param string year in format CCYY, default is current local year
  477. * @param boolean onOrAfter if true and days are same, returns current day
  478. * @param string format for returned date
  479. *
  480. * @access public
  481. *
  482. * @return string date in given format
  483. */
  484. function nextDayOfWeek($dow,$day='',$month='',$year='',$format='%Y%m%d',$onOrAfter=false)
  485. {
  486. if (empty($year)) {
  487. $year = Date_Calc::dateNow('%Y');
  488. }
  489. if (empty($month)) {
  490. $month = Date_Calc::dateNow('%m');
  491. }
  492. if (empty($day)) {
  493. $day = Date_Calc::dateNow('%d');
  494. }
  495. $days = Date_Calc::dateToDays($day,$month,$year);
  496. $curr_weekday = Date_Calc::dayOfWeek($day,$month,$year);
  497. if ($curr_weekday == $dow) {
  498. if (!$onOrAfter) {
  499. $days += 7;
  500. }
  501. }
  502. elseif ($curr_weekday > $dow) {
  503. $days += 7 - ( $curr_weekday - $dow );
  504. } else {
  505. $days += $dow - $curr_weekday;
  506. }
  507. return(Date_Calc::daysToDate($days,$format));
  508. } // end func nextDayOfWeek
  509. /**
  510. * Returns date of the previous specific day of the week
  511. * from the given date.
  512. *
  513. * @param int day of week, 0=Sunday
  514. * @param string day in format DD, default is current local day
  515. * @param string month in format MM, default is current local month
  516. * @param string year in format CCYY, default is current local year
  517. * @param boolean onOrBefore if true and days are same, returns current day
  518. * @param string format for returned date
  519. *
  520. * @access public
  521. *
  522. * @return string date in given format
  523. */
  524. function prevDayOfWeek($dow,$day='',$month='',$year='',$format='%Y%m%d',$onOrBefore=false)
  525. {
  526. if (empty($year)) {
  527. $year = Date_Calc::dateNow('%Y');
  528. }
  529. if (empty($month)) {
  530. $month = Date_Calc::dateNow('%m');
  531. }
  532. if (empty($day)) {
  533. $day = Date_Calc::dateNow('%d');
  534. }
  535. $days = Date_Calc::dateToDays($day,$month,$year);
  536. $curr_weekday = Date_Calc::dayOfWeek($day,$month,$year);
  537. if ($curr_weekday == $dow) {
  538. if (!$onOrBefore) {
  539. $days -= 7;
  540. }
  541. }
  542. elseif ($curr_weekday < $dow) {
  543. $days -= 7 - ( $dow - $curr_weekday );
  544. } else {
  545. $days -= $curr_weekday - $dow;
  546. }
  547. return(Date_Calc::daysToDate($days,$format));
  548. } // end func prevDayOfWeek
  549. /**
  550. * Returns date of the next specific day of the week
  551. * on or after the given date.
  552. *
  553. * @param int day of week, 0=Sunday
  554. * @param string day in format DD, default is current local day
  555. * @param string month in format MM, default is current local month
  556. * @param string year in format CCYY, default is current local year
  557. * @param string format for returned date
  558. *
  559. * @access public
  560. *
  561. * @return string date in given format
  562. */
  563. function nextDayOfWeekOnOrAfter($dow,$day='',$month='',$year='',$format='%Y%m%d')
  564. {
  565. return(Date_Calc::nextDayOfWeek($dow,$day,$month,$year,$format,true));
  566. } // end func nextDayOfWeekOnOrAfter
  567. /**
  568. * Returns date of the previous specific day of the week
  569. * on or before the given date.
  570. *
  571. * @param int day of week, 0=Sunday
  572. * @param string day in format DD, default is current local day
  573. * @param string month in format MM, default is current local month
  574. * @param string year in format CCYY, default is current local year
  575. * @param string format for returned date
  576. *
  577. * @access public
  578. *
  579. * @return string date in given format
  580. */
  581. function prevDayOfWeekOnOrBefore($dow,$day='',$month='',$year='',$format='%Y%m%d')
  582. {
  583. return(Date_Calc::prevDayOfWeek($dow,$day,$month,$year,$format,true));
  584. } // end func prevDayOfWeekOnOrAfter
  585. /**
  586. * Returns date of day after given date.
  587. *
  588. * @param string day in format DD, default is current local day
  589. * @param string month in format MM, default is current local month
  590. * @param string year in format CCYY, default is current local year
  591. * @param string format for returned date
  592. *
  593. * @access public
  594. *
  595. * @return string date in given format
  596. */
  597. function nextDay($day='',$month='',$year='',$format='%Y%m%d')
  598. {
  599. if (empty($year)) {
  600. $year = Date_Calc::dateNow('%Y');
  601. }
  602. if (empty($month)) {
  603. $month = Date_Calc::dateNow('%m');
  604. }
  605. if (empty($day)) {
  606. $day = Date_Calc::dateNow('%d');
  607. }
  608. $days = Date_Calc::dateToDays($day,$month,$year);
  609. return(Date_Calc::daysToDate($days + 1,$format));
  610. } // end func nextDay
  611. /**
  612. * Returns date of day before given date.
  613. *
  614. * @param string day in format DD, default is current local day
  615. * @param string month in format MM, default is current local month
  616. * @param string year in format CCYY, default is current local year
  617. * @param string format for returned date
  618. *
  619. * @access public
  620. *
  621. * @return string date in given format
  622. */
  623. function prevDay($day='',$month='',$year='',$format='%Y%m%d')
  624. {
  625. if (empty($year)) {
  626. $year = Date_Calc::dateNow('%Y');
  627. }
  628. if (empty($month)) {
  629. $month = Date_Calc::dateNow('%m');
  630. }
  631. if (empty($day)) {
  632. $day = Date_Calc::dateNow('%d');
  633. }
  634. $days = Date_Calc::dateToDays($day,$month,$year);
  635. return(Date_Calc::daysToDate($days - 1,$format));
  636. } // end func prevDay
  637. /**
  638. * Sets century for 2 digit year.
  639. * 51-99 is 19, else 20
  640. *
  641. * @param string 2 digit year
  642. *
  643. * @access public
  644. *
  645. * @return string 4 digit year
  646. */
  647. function defaultCentury($year)
  648. {
  649. if (strlen($year) == 1) {
  650. $year = "0$year";
  651. }
  652. if ($year > 50) {
  653. return( "19$year" );
  654. } else {
  655. return( "20$year" );
  656. }
  657. } // end func defaultCentury
  658. /**
  659. * Returns number of days between two given dates.
  660. *
  661. * @param string $day1 day in format DD
  662. * @param string $month1 month in format MM
  663. * @param string $year1 year in format CCYY
  664. * @param string $day2 day in format DD
  665. * @param string $month2 month in format MM
  666. * @param string $year2 year in format CCYY
  667. *
  668. * @access public
  669. *
  670. * @return int absolute number of days between dates,
  671. * -1 if there is an error.
  672. */
  673. function dateDiff($day1,$month1,$year1,$day2,$month2,$year2)
  674. {
  675. if (!Date_Calc::isValidDate($day1,$month1,$year1)) {
  676. return -1;
  677. }
  678. if (!Date_Calc::isValidDate($day2,$month2,$year2)) {
  679. return -1;
  680. }
  681. return(abs((Date_Calc::dateToDays($day1,$month1,$year1))
  682. - (Date_Calc::dateToDays($day2,$month2,$year2))));
  683. } // end func dateDiff
  684. /**
  685. * Compares two dates
  686. *
  687. * @param string $day1 day in format DD
  688. * @param string $month1 month in format MM
  689. * @param string $year1 year in format CCYY
  690. * @param string $day2 day in format DD
  691. * @param string $month2 month in format MM
  692. * @param string $year2 year in format CCYY
  693. *
  694. * @access public
  695. * @return int 0 on equality, 1 if date 1 is greater, -1 if smaller
  696. */
  697. function compareDates($day1,$month1,$year1,$day2,$month2,$year2)
  698. {
  699. $ndays1 = Date_Calc::dateToDays($day1, $month1, $year1);
  700. $ndays2 = Date_Calc::dateToDays($day2, $month2, $year2);
  701. if ($ndays1 == $ndays2) {
  702. return 0;
  703. }
  704. return ($ndays1 > $ndays2) ? 1 : -1;
  705. } // end func compareDates
  706. /**
  707. * Find the number of days in the given month.
  708. *
  709. * @param string month in format MM, default is current local month
  710. * @param string year in format CCYY, default is current local year
  711. *
  712. * @access public
  713. *
  714. * @return int number of days
  715. */
  716. function daysInMonth($month='',$year='')
  717. {
  718. if (empty($year)) {
  719. $year = Date_Calc::dateNow('%Y');
  720. }
  721. if (empty($month)) {
  722. $month = Date_Calc::dateNow('%m');
  723. }
  724. if ($year == 1582 && $month == 10) {
  725. return 21; // October 1582 only had 1st-4th and 15th-31st
  726. }
  727. if ($month == 2) {
  728. if (Date_Calc::isLeapYear($year)) {
  729. return 29;
  730. } else {
  731. return 28;
  732. }
  733. } elseif ($month == 4 or $month == 6 or $month == 9 or $month == 11) {
  734. return 30;
  735. } else {
  736. return 31;
  737. }
  738. } // end func daysInMonth
  739. /**
  740. * Returns the number of rows on a calendar month. Useful for
  741. * determining the number of rows when displaying a typical
  742. * month calendar.
  743. *
  744. * @param string month in format MM, default is current local month
  745. * @param string year in format CCYY, default is current local year
  746. *
  747. * @access public
  748. *
  749. * @return int number of weeks
  750. */
  751. function weeksInMonth($month='',$year='')
  752. {
  753. if (empty($year)) {
  754. $year = Date_Calc::dateNow('%Y');
  755. }
  756. if (empty($month)) {
  757. $month = Date_Calc::dateNow('%m');
  758. }
  759. $FDOM = Date_Calc::firstOfMonthWeekday($month, $year);
  760. if ($FDOM > DATE_CALC_BEGIN_WEEKDAY) {
  761. $first_week_days = 7 - $FDOM + DATE_CALC_BEGIN_WEEKDAY;
  762. $weeks = 1;
  763. } else {
  764. $first_week_days = DATE_CALC_BEGIN_WEEKDAY - $FDOM;
  765. $weeks = 0;
  766. }
  767. $first_week_days %= 7;
  768. return (ceil((Date_Calc::daysInMonth($month, $year) - $first_week_days) / 7) + $weeks);
  769. } // end func weeksInMonth
  770. /**
  771. * Find the day of the week for the first of the month of given date.
  772. *
  773. * @param string month in format MM, default is current local month
  774. * @param string year in format CCYY, default is current local year
  775. *
  776. * @access public
  777. *
  778. * @return int number of weekday for the first day, 0=Sunday
  779. */
  780. function firstOfMonthWeekday($month='',$year='')
  781. {
  782. if (empty($year)) {
  783. $year = Date_Calc::dateNow('%Y');
  784. }
  785. if (empty($month)) {
  786. $month = Date_Calc::dateNow('%m');
  787. }
  788. return(Date_Calc::dayOfWeek('01',$month,$year));
  789. } // end func firstOfMonthWeekday
  790. /**
  791. * Return date of first day of month of given date.
  792. *
  793. * @param string month in format MM, default is current local month
  794. * @param string year in format CCYY, default is current local year
  795. * @param string format for returned date
  796. *
  797. * @access public
  798. *
  799. * @return string date in given format
  800. */
  801. function beginOfMonth($month='',$year='',$format='%Y%m%d')
  802. {
  803. if (empty($year)) {
  804. $year = Date_Calc::dateNow('%Y');
  805. }
  806. if (empty($month)) {
  807. $month = Date_Calc::dateNow('%m');
  808. }
  809. return(Date_Calc::dateFormat('01',$month,$year,$format));
  810. } // end of func beginOfMonth
  811. /**
  812. * Find the month day of the beginning of week for given date,
  813. * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.)
  814. *
  815. * @param string day in format DD, default is current local day
  816. * @param string month in format MM, default is current local month
  817. * @param string year in format CCYY, default is current local year
  818. * @param string format for returned date
  819. *
  820. * @access public
  821. *
  822. * @return string date in given format
  823. */
  824. function beginOfWeek($day='',$month='',$year='',$format='%Y%m%d')
  825. {
  826. if (empty($year)) {
  827. $year = Date_Calc::dateNow('%Y');
  828. }
  829. if (empty($month)) {
  830. $month = Date_Calc::dateNow('%m');
  831. }
  832. if (empty($day)) {
  833. $day = Date_Calc::dateNow('%d');
  834. }
  835. $this_weekday = Date_Calc::dayOfWeek($day,$month,$year);
  836. if (DATE_CALC_BEGIN_WEEKDAY == 1) {
  837. if ($this_weekday == 0) {
  838. $beginOfWeek = Date_Calc::dateToDays($day,$month,$year) - 6;
  839. } else {
  840. $beginOfWeek = Date_Calc::dateToDays($day,$month,$year)
  841. - $this_weekday + 1;
  842. }
  843. } else {
  844. $beginOfWeek = (Date_Calc::dateToDays($day,$month,$year)
  845. - $this_weekday);
  846. }
  847. /* $beginOfWeek = (Date_Calc::dateToDays($day,$month,$year)
  848. - ($this_weekday - DATE_CALC_BEGIN_WEEKDAY)); */
  849. return(Date_Calc::daysToDate($beginOfWeek,$format));
  850. } // end of func beginOfWeek
  851. /**
  852. * Find the month day of the end of week for given date,
  853. * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday
  854. * of following month.)
  855. *
  856. * @param string day in format DD, default is current local day
  857. * @param string month in format MM, default is current local month
  858. * @param string year in format CCYY, default is current local year
  859. * @param string format for returned date
  860. *
  861. * @access public
  862. *
  863. * @return string date in given format
  864. */
  865. function endOfWeek($day='',$month='',$year='',$format='%Y%m%d')
  866. {
  867. if (empty($year)) {
  868. $year = Date_Calc::dateNow('%Y');
  869. }
  870. if (empty($month)) {
  871. $month = Date_Calc::dateNow('%m');
  872. }
  873. if (empty($day)) {
  874. $day = Date_Calc::dateNow('%d');
  875. }
  876. $this_weekday = Date_Calc::dayOfWeek($day,$month,$year);
  877. $last_dayOfWeek = (Date_Calc::dateToDays($day,$month,$year)
  878. + (6 - $this_weekday + DATE_CALC_BEGIN_WEEKDAY));
  879. return(Date_Calc::daysToDate($last_dayOfWeek,$format));
  880. } // end func endOfWeek
  881. /**
  882. * Find the month day of the beginning of week after given date,
  883. * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.)
  884. *
  885. * @param string day in format DD, default is current local day
  886. * @param string month in format MM, default is current local month
  887. * @param string year in format CCYY, default is current local year
  888. * @param string format for returned date
  889. *
  890. * @access public
  891. *
  892. * @return string date in given format
  893. */
  894. function beginOfNextWeek($day='',$month='',$year='',$format='%Y%m%d')
  895. {
  896. if (empty($year)) {
  897. $year = Date_Calc::dateNow("%Y");
  898. }
  899. if (empty($month)) {
  900. $month = Date_Calc::dateNow("%m");
  901. }
  902. if (empty($day)) {
  903. $day = Date_Calc::dateNow("%d");
  904. }
  905. $date = Date_Calc::daysToDate(
  906. Date_Calc::dateToDays($day+7,$month,$year),"%Y%m%d"
  907. );
  908. $next_week_year = substr($date,0,4);
  909. $next_week_month = substr($date,4,2);
  910. $next_week_day = substr($date,6,2);
  911. return Date_Calc::beginOfWeek(
  912. $next_week_day,$next_week_month,$next_week_year,
  913. $format
  914. );
  915. } // end func beginOfNextWeek
  916. /**
  917. * Find the month day of the beginning of week before given date,
  918. * using DATE_CALC_BEGIN_WEEKDAY. (can return weekday of prev month.)
  919. *
  920. * @param string day in format DD, default is current local day
  921. * @param string month in format MM, default is current local month
  922. * @param string year in format CCYY, default is current local year
  923. * @param string format for returned date
  924. *
  925. * @access public
  926. *
  927. * @return string date in given format
  928. */
  929. function beginOfPrevWeek($day='',$month='',$year='',$format='%Y%m%d')
  930. {
  931. if (empty($year)) {
  932. $year = Date_Calc::dateNow("%Y");
  933. }
  934. if (empty($month)) {
  935. $month = Date_Calc::dateNow("%m");
  936. }
  937. if (empty($day)) {
  938. $day = Date_Calc::dateNow("%d");
  939. }
  940. $date = Date_Calc::daysToDate(
  941. Date_Calc::dateToDays($day-7,$month,$year),"%Y%m%d"
  942. );
  943. $prev_week_year = substr($date,0,4);
  944. $prev_week_month = substr($date,4,2);
  945. $prev_week_day = substr($date,6,2);
  946. return Date_Calc::beginOfWeek($prev_week_day,$prev_week_month,$prev_week_year,$format);
  947. } // end func beginOfPrevWeek
  948. /**
  949. * Return an array with days in week
  950. *
  951. * @param string day in format DD, default is current local day
  952. * @param string month in format MM, default is current local month
  953. * @param string year in format CCYY, default is current local year
  954. * @param string format for returned date
  955. *
  956. * @access public
  957. *
  958. * @return array $week[$weekday]
  959. */
  960. function getCalendarWeek($day='',$month='',$year='',$format='%Y%m%d')
  961. {
  962. if (empty($year)) {
  963. $year = Date_Calc::dateNow('%Y');
  964. }
  965. if (empty($month)) {
  966. $month = Date_Calc::dateNow('%m');
  967. }
  968. if (empty($day)) {
  969. $day = Date_Calc::dateNow('%d');
  970. }
  971. $week_array = array();
  972. // date for the column of week
  973. $curr_day = Date_Calc::beginOfWeek($day,$month,$year,'%E');
  974. for($counter=0; $counter <= 6; $counter++) {
  975. $week_array[$counter] = Date_Calc::daysToDate($curr_day,$format);
  976. $curr_day++;
  977. }
  978. return $week_array;
  979. } // end func getCalendarWeek
  980. /**
  981. * Return a set of arrays to construct a calendar month for
  982. * the given date.
  983. *
  984. * @param string month in format MM, default is current local month
  985. * @param string year in format CCYY, default is current local year
  986. * @param string format for returned date
  987. *
  988. * @access public
  989. *
  990. * @return array $month[$row][$col]
  991. */
  992. function getCalendarMonth($month='',$year='',$format='%Y%m%d')
  993. {
  994. if (empty($year)) {
  995. $year = Date_Calc::dateNow('%Y');
  996. }
  997. if (empty($month)) {
  998. $month = Date_Calc::dateNow('%m');
  999. }
  1000. $month_array = array();
  1001. // date for the first row, first column of calendar month
  1002. if (DATE_CALC_BEGIN_WEEKDAY == 1) {
  1003. if (Date_Calc::firstOfMonthWeekday($month,$year) == 0) {
  1004. $curr_day = Date_Calc::dateToDays('01',$month,$year) - 6;
  1005. } else {
  1006. $curr_day = Date_Calc::dateToDays('01',$month,$year)
  1007. - Date_Calc::firstOfMonthWeekday($month,$year) + 1;
  1008. }
  1009. } else {
  1010. $curr_day = (Date_Calc::dateToDays('01',$month,$year)
  1011. - Date_Calc::firstOfMonthWeekday($month,$year));
  1012. }
  1013. // number of days in this month
  1014. $daysInMonth = Date_Calc::daysInMonth($month,$year);
  1015. $weeksInMonth = Date_Calc::weeksInMonth($month,$year);
  1016. for($row_counter=0; $row_counter < $weeksInMonth; $row_counter++) {
  1017. for($column_counter=0; $column_counter <= 6; $column_counter++) {
  1018. $month_array[$row_counter][$column_counter] =
  1019. Date_Calc::daysToDate($curr_day,$format);
  1020. $curr_day++;
  1021. }
  1022. }
  1023. return $month_array;
  1024. } // end func getCalendarMonth
  1025. /**
  1026. * Return a set of arrays to construct a calendar year for
  1027. * the given date.
  1028. *
  1029. * @param string year in format CCYY, default current local year
  1030. * @param string format for returned date
  1031. *
  1032. * @access public
  1033. *
  1034. * @return array $year[$month][$row][$col]
  1035. */
  1036. function getCalendarYear($year='',$format='%Y%m%d')
  1037. {
  1038. if (empty($year)) {
  1039. $year = Date_Calc::dateNow('%Y');
  1040. }
  1041. $year_array = array();
  1042. for($curr_month=0; $curr_month <=11; $curr_month++) {
  1043. $year_array[$curr_month] =
  1044. Date_Calc::getCalendarMonth(sprintf('%02d',$curr_month+1),$year,$format);
  1045. }
  1046. return $year_array;
  1047. } // end func getCalendarYear
  1048. /**
  1049. * Converts a date to number of days since a
  1050. * distant unspecified epoch.
  1051. *
  1052. * @param string day in format DD
  1053. * @param string month in format MM
  1054. * @param string year in format CCYY
  1055. *
  1056. * @access public
  1057. *
  1058. * @return integer number of days
  1059. */
  1060. function dateToDays($day,$month,$year)
  1061. {
  1062. $century = (int) substr($year,0,2);
  1063. $year = (int) substr($year,2,2);
  1064. if ($month > 2) {
  1065. $month -= 3;
  1066. } else {
  1067. $month += 9;
  1068. if ($year) {
  1069. $year--;
  1070. } else {
  1071. $year = 99;
  1072. $century --;
  1073. }
  1074. }
  1075. return (floor(( 146097 * $century) / 4 ) +
  1076. floor(( 1461 * $year) / 4 ) +
  1077. floor(( 153 * $month + 2) / 5 ) +
  1078. $day + 1721119);
  1079. } // end func dateToDays
  1080. /**
  1081. * Converts number of days to a distant unspecified epoch.
  1082. *
  1083. * @param int number of days
  1084. * @param string format for returned date
  1085. *
  1086. * @access public
  1087. *
  1088. * @return string date in specified format
  1089. */
  1090. function daysToDate($days,$format='%Y%m%d')
  1091. {
  1092. $days -= 1721119;
  1093. $century = floor(( 4 * $days - 1) / 146097);
  1094. $days = floor(4 * $days - 1 - 146097 * $century);
  1095. $day = floor($days / 4);
  1096. $year = floor(( 4 * $day + 3) / 1461);
  1097. $day = floor(4 * $day + 3 - 1461 * $year);
  1098. $day = floor(($day + 4) / 4);
  1099. $month = floor(( 5 * $day - 3) / 153);
  1100. $day = floor(5 * $day - 3 - 153 * $month);
  1101. $day = floor(($day + 5) / 5);
  1102. if ($month < 10) {
  1103. $month +=3;
  1104. } else {
  1105. $month -=9;
  1106. if ($year++ == 99) {
  1107. $year = 0;
  1108. $century++;
  1109. }
  1110. }
  1111. $century = sprintf('%02d',$century);
  1112. $year = sprintf('%02d',$year);
  1113. return(Date_Calc::dateFormat($day,$month,$century.$year,$format));
  1114. } // end func daysToDate
  1115. /**
  1116. * Calculates the date of the Nth weekday of the month,
  1117. * such as the second Saturday of January 2000.
  1118. *
  1119. * @param string occurance: 1=first, 2=second, 3=third, etc.
  1120. * @param string dayOfWeek: 0=Sunday, 1=Monday, etc.
  1121. * @param string month in format MM
  1122. * @param string year in format CCYY
  1123. * @param string format for returned date
  1124. *
  1125. * @access public
  1126. *
  1127. * @return string date in given format
  1128. */
  1129. function NWeekdayOfMonth($occurance,$dayOfWeek,$month,$year,$format='%Y%m%d')
  1130. {
  1131. $year = sprintf('%04d',$year);
  1132. $month = sprintf('%02d',$month);
  1133. $DOW1day = sprintf('%02d',(($occurance - 1) * 7 + 1));
  1134. $DOW1 = Date_Calc::dayOfWeek($DOW1day,$month,$year);
  1135. $wdate = ($occurance - 1) * 7 + 1 +
  1136. (7 + $dayOfWeek - $DOW1) % 7;
  1137. if ( $wdate > Date_Calc::daysInMonth($month,$year)) {
  1138. return -1;
  1139. } else {
  1140. return(Date_Calc::dateFormat($wdate,$month,$year,$format));
  1141. }
  1142. } // end func NWeekdayOfMonth
  1143. /**
  1144. * Formats the date in the given format, much like
  1145. * strfmt(). This function is used to alleviate the
  1146. * problem with 32-bit numbers for dates pre 1970
  1147. * or post 2038, as strfmt() has on most systems.
  1148. * Most of the formatting options are compatible.
  1149. *
  1150. * formatting options:
  1151. *
  1152. * %a abbreviated weekday name (Sun, Mon, Tue)
  1153. * %A full weekday name (Sunday, Monday, Tuesday)
  1154. * %b abbreviated month name (Jan, Feb, Mar)
  1155. * %B full month name (January, February, March)
  1156. * %d day of month (range 00 to 31)
  1157. * %e day of month, single digit (range 0 to 31)
  1158. * %E number of days since unspecified epoch (integer)
  1159. * (%E is useful for passing a date in a URL as
  1160. * an integer value. Then simply use
  1161. * daysToDate() to convert back to a date.)
  1162. * %j day of year (range 001 to 366)
  1163. * %m month as decimal number (range 1 to 12)
  1164. * %n newline character (\n)
  1165. * %t tab character (\t)
  1166. * %w weekday as decimal (0 = Sunday)
  1167. * %U week number of current year, first sunday as first week
  1168. * %y year as decimal (range 00 to 99)
  1169. * %Y year as decimal including century (range 0000 to 9999)
  1170. * %% literal '%'
  1171. *
  1172. * @param string day in format DD
  1173. * @param string month in format MM
  1174. * @param string year in format CCYY
  1175. * @param string format for returned date
  1176. *
  1177. * @access public
  1178. *
  1179. * @return string date in given format
  1180. */
  1181. function dateFormat($day,$month,$year,$format)
  1182. {
  1183. if (!Date_Calc::isValidDate($day,$month,$year)) {
  1184. $year = Date_Calc::dateNow('%Y');
  1185. $month = Date_Calc::dateNow('%m');
  1186. $day = Date_Calc::dateNow('%d');
  1187. }
  1188. $output = '';
  1189. for($strpos = 0; $strpos < strlen($format); $strpos++) {
  1190. $char = substr($format,$strpos,1);
  1191. if ($char == '%') {
  1192. $nextchar = substr($format,$strpos + 1,1);
  1193. switch($nextchar) {
  1194. case 'a':
  1195. $output .= Date_Calc::getWeekdayAbbrname($day,$month,$year);
  1196. break;
  1197. case 'A':
  1198. $output .= Date_Calc::getWeekdayFullname($day,$month,$year);
  1199. break;
  1200. case 'b':
  1201. $output .= Date_Calc::getMonthAbbrname($month);
  1202. break;
  1203. case 'B':
  1204. $output .= Date_Calc::getMonthFullname($month);
  1205. break;
  1206. case 'd':
  1207. $output .= sprintf('%02d',$day);
  1208. break;
  1209. case 'e':
  1210. $output .= $day;
  1211. break;
  1212. case 'E':
  1213. $output .= Date_Calc::dateToDays($day,$month,$year);
  1214. break;
  1215. case 'j':
  1216. $output .= Date_Calc::julianDate($day,$month,$year);
  1217. break;
  1218. case 'm':
  1219. $output .= sprintf('%02d',$month);
  1220. break;
  1221. case 'n':
  1222. $output .= "\n";
  1223. break;
  1224. case "t":
  1225. $output .= "\t";
  1226. break;
  1227. case 'w':
  1228. $output .= Date_Calc::dayOfWeek($day,$month,$year);
  1229. break;
  1230. case 'U':
  1231. $output .= Date_Calc::weekOfYear($day,$month,$year);
  1232. break;
  1233. case 'y':
  1234. $output .= substr($year,2,2);
  1235. break;
  1236. case 'Y':
  1237. $output .= $year;
  1238. break;
  1239. case '%':
  1240. $output .= '%';
  1241. break;
  1242. default:
  1243. $output .= $char.$nextchar;
  1244. }
  1245. $strpos++;
  1246. } else {
  1247. $output .= $char;
  1248. }
  1249. }
  1250. return $output;
  1251. } // end func dateFormat
  1252. /**
  1253. * Returns the current local year in format CCYY
  1254. *
  1255. * @access public
  1256. *
  1257. * @return string year in format CCYY
  1258. */
  1259. function getYear()
  1260. {
  1261. return Date_Calc::dateNow('%Y');
  1262. } // end func getYear
  1263. /**
  1264. * Returns the current local month in format MM
  1265. *
  1266. * @access public
  1267. *
  1268. * @return string month in format MM
  1269. */
  1270. function getMonth()
  1271. {
  1272. return Date_Calc::dateNow('%m');
  1273. } // end func getMonth
  1274. /**
  1275. * Returns the current local day in format DD
  1276. *
  1277. * @access public
  1278. *
  1279. * @return string day in format DD
  1280. */
  1281. function getDay()
  1282. {
  1283. return Date_Calc::dateNow('%d');
  1284. } // end func getDay
  1285. /**
  1286. * Returns the full month name for the given month
  1287. *
  1288. * @param string month in format MM
  1289. *
  1290. * @access public
  1291. *
  1292. * @return string full month name
  1293. */
  1294. function getMonthFullname($month)
  1295. {
  1296. $month = (int)$month;
  1297. if (empty($month)) {
  1298. $month = (int) Date_Calc::dateNow('%m');
  1299. }
  1300. $month_names = Date_Calc::getMonthNames();
  1301. return $month_names[$month];
  1302. // getMonthNames returns months with correct indexes
  1303. //return $month_names[($month - 1)];
  1304. } // end func getMonthFullname
  1305. /**
  1306. * Returns the abbreviated month name for the given month
  1307. *
  1308. * @param string month in format MM
  1309. * @param int optional length of abbreviation, default is 3
  1310. *
  1311. * @access public
  1312. *
  1313. * @return string abbreviated month name
  1314. * @see Date_Calc::getMonthFullname
  1315. */
  1316. function getMonthAbbrname($month,$length=3)
  1317. {
  1318. $month = (int)$month;
  1319. if (empty($month)) {
  1320. $month = Date_Calc::dateNow('%m');
  1321. }
  1322. return substr(Date_Calc::getMonthFullname($month), 0, $length);
  1323. } // end func getMonthAbbrname
  1324. /**
  1325. * Returns the full weekday name for the given date
  1326. *
  1327. * @param string day in format DD, default is current local day
  1328. * @param string month in format MM, default is current local month
  1329. * @param string year in format CCYY, default is current local year
  1330. *
  1331. * @access public
  1332. *
  1333. * @return string full month name
  1334. */
  1335. function getWeekdayFullname($day='',$month='',$year='')
  1336. {
  1337. if (empty($year)) {
  1338. $year = Date_Calc::dateNow('%Y');
  1339. }
  1340. if (empty($month)) {
  1341. $month = Date_Calc::dateNow('%m');
  1342. }
  1343. if (empty($day)) {
  1344. $day = Date_Calc::dateNow('%d');
  1345. }
  1346. $weekday_names = Date_Calc::getWeekDays();
  1347. $weekday = Date_Calc::dayOfWeek($day,$month,$year);
  1348. return $weekday_names[$weekday];
  1349. } // end func getWeekdayFullname
  1350. /**
  1351. * Returns the abbreviated weekday name for the given date
  1352. *
  1353. * @param string day in format DD, default is current local day
  1354. * @param string month in format MM, default is current local month
  1355. * @param string year in format CCYY, default is current local year
  1356. * @param int optional length of abbreviation, default is 3
  1357. *
  1358. * @access public
  1359. *
  1360. * @return string full month name
  1361. * @see Date_Calc::getWeekdayFullname
  1362. */
  1363. function getWeekdayAbbrname($day='',$month='',$year='',$length=3)
  1364. {
  1365. if (empty($year)) {
  1366. $year = Date_Calc::dateNow('%Y');
  1367. }
  1368. if (empty($month)) {
  1369. $month = Date_Calc::dateNow('%m');
  1370. }
  1371. if (empty($day)) {
  1372. $day = Date_Calc::dateNow('%d');
  1373. }
  1374. return substr(Date_Calc::getWeekdayFullname($day,$month,$year),0,$length);
  1375. } // end func getWeekdayFullname
  1376. /**
  1377. * Returns the numeric month from the month name or an abreviation
  1378. *
  1379. * Both August and Aug would return 8.
  1380. * Month name is case insensitive.
  1381. *
  1382. * @param string month name
  1383. * @return integer month number
  1384. */
  1385. function getMonthFromFullName($month)
  1386. {
  1387. $month = strtolower($month);
  1388. $months = Date_Calc::getMonthNames();
  1389. while(list($id, $name) = each($months)) {
  1390. if (ereg($month, strtolower($name))) {
  1391. return($id);
  1392. }
  1393. }
  1394. return(0);
  1395. } // end func getMonthFromFullName
  1396. /**
  1397. * Returns an array of month names
  1398. *
  1399. * Used to take advantage of the setlocale function to return
  1400. * language specific month names.
  1401. * XXX cache values to some global array to avoid preformace
  1402. * hits when called more than once.
  1403. *
  1404. * @returns array An array of month names
  1405. */
  1406. function getMonthNames()
  1407. {
  1408. for($i=1;$i<13;$i++) {
  1409. $months[$i] = strftime('%B', mktime(0, 0, 0, $i, 1, 2001));
  1410. }
  1411. return($months);
  1412. } // end func getMonthNames
  1413. /**
  1414. * Returns an array of week days
  1415. *
  1416. * Used to take advantage of the setlocale function to
  1417. * return language specific week days
  1418. * XXX cache values to some global array to avoid preformace
  1419. * hits when called more than once.
  1420. *
  1421. * @returns array An array of week day names
  1422. */
  1423. function getWeekDays()
  1424. {
  1425. for($i=0;$i<7;$i++) {
  1426. $weekdays[$i] = strftime('%A', mktime(0, 0, 0, 1, $i, 2001));
  1427. }
  1428. return($weekdays);
  1429. } // end func getWeekDays
  1430. /**
  1431. * Converts from Gregorian Year-Month-Day to
  1432. * ISO YearNumber-WeekNumber-WeekDay
  1433. *
  1434. * Uses ISO 8601 definitions.
  1435. * Algorithm from Rick McCarty, 1999 at
  1436. * http://personal.ecu.edu/mccartyr/ISOwdALG.txt
  1437. *
  1438. * @param string day in format DD
  1439. * @param string month in format MM
  1440. * @param string year in format CCYY
  1441. * @return string
  1442. * @access public
  1443. */
  1444. // Transcribed to PHP by Jesus M. Castagnetto (blame him if it is fubared ;-)
  1445. function gregorianToISO($day,$month,$year) {
  1446. $mnth = array (0,31,59,90,120,151,181,212,243,273,304,334);
  1447. $y_isleap = Date_Calc::isLeapYear($year);
  1448. $y_1_isleap = Date_Calc::isLeapYear($year - 1);
  1449. $day_of_year_number = $day + $mnth[$month - 1];
  1450. if ($y_isleap && $month > 2) {
  1451. $day_of_year_number++;
  1452. }
  1453. // find Jan 1 weekday (monday = 1, sunday = 7)
  1454. $yy = ($year - 1) % 100;
  1455. $c = ($year - 1) - $yy;
  1456. $g = $yy + intval($yy/4);
  1457. $jan1_weekday = 1 + intval((((($c / 100) % 4) * 5) + $g) % 7);
  1458. // weekday for year-month-day
  1459. $h = $day_of_year_number + ($jan1_weekday - 1);
  1460. $weekday = 1 + intval(($h - 1) % 7);
  1461. // find if Y M D falls in YearNumber Y-1, WeekNumber 52 or
  1462. if ($day_of_year_number <= (8 - $jan1_weekday) && $jan1_weekday > 4){
  1463. $yearnumber = $year - 1;
  1464. if ($jan1_weekday == 5 || ($jan1_weekday == 6 && $y_1_isleap)) {
  1465. $weeknumber = 53;
  1466. } else {
  1467. $weeknumber = 52;
  1468. }
  1469. } else {
  1470. $yearnumber = $year;
  1471. }
  1472. // find if Y M D falls in YearNumber Y+1, WeekNumber 1
  1473. if ($yearnumber == $year) {
  1474. if ($y_isleap) {
  1475. $i = 366;
  1476. } else {
  1477. $i = 365;
  1478. }
  1479. if (($i - $day_of_year_number) < (4 - $weekday)) {
  1480. $yearnumber++;
  1481. $weeknumber = 1;
  1482. }
  1483. }
  1484. // find if Y M D falls in YearNumber Y, WeekNumber 1 through 53
  1485. if ($yearnumber == $year) {
  1486. $j = $day_of_year_number + (7 - $weekday) + ($jan1_weekday - 1);
  1487. $weeknumber = intval($j / 7);
  1488. if ($jan1_weekday > 4) {
  1489. $weeknumber--;
  1490. }
  1491. }
  1492. // put it all together
  1493. if ($weeknumber < 10)
  1494. $weeknumber = '0'.$weeknumber;
  1495. return "{$yearnumber}-{$weeknumber}-{$weekday}";
  1496. }
  1497. /**
  1498. * Determines julian date of the given season
  1499. * Adapted from previous work in Java by James Mark Hamilton, mhamilton@qwest.net
  1500. *
  1501. * @author Robert Butler <rob@maxwellcreek.org>
  1502. *
  1503. * @param string is VERNALEQUINOX, SUMMERSOLSTICE, AUTUMNALEQUINOX, or WINTERSOLSTICE.
  1504. * @param string year in format CCYY, must be a calendar year between -1000BC and 3000AD.
  1505. *
  1506. * @access public
  1507. *
  1508. * @return float $juliandate
  1509. */
  1510. function dateSeason ($season, $year = '') {
  1511. if ($year == '') {
  1512. $year = Date_Calc::dateNow('%Y');
  1513. }
  1514. if (($year >= -1000) && ($year <= 1000)) {
  1515. $y = $year / 1000.0;
  1516. if ($season == 'VERNALEQUINOX') {
  1517. $juliandate = (((((((-0.00071 * $y) - 0.00111) * $y) + 0.06134) * $y) + 365242.1374) * $y) + 1721139.29189;
  1518. } else if ($season == 'SUMMERSOLSTICE') {
  1519. $juliandate = ((((((( 0.00025 * $y) + 0.00907) * $y) - 0.05323) * $y) + 365241.72562) * $y) + 1721233.25401;
  1520. } else if ($season == 'AUTUMNALEQUINOX') {
  1521. $juliandate = ((((((( 0.00074 * $y) - 0.00297) * $y) - 0.11677) * $y) + 365242.49558) * $y) + 1721325.70455;
  1522. } else if ($season == 'WINTERSOLSTICE') {
  1523. $juliandate = (((((((-0.00006 * $y) - 0.00933) * $y) - 0.00769) * $y) + 365242.88257) * $y) + 1721414.39987;
  1524. }
  1525. } elseif (($year > 1000) && ($year <= 3000)) {
  1526. $y = ($year - 2000) / 1000;
  1527. if ($season == 'VERNALEQUINOX') {
  1528. $juliandate = (((((((-0.00057 * $y) - 0.00411) * $y) + 0.05169) * $y) + 365242.37404) * $y) + 2451623.80984;
  1529. } else if ($season == 'SUMMERSOLSTICE') {
  1530. $juliandate = (((((((-0.0003 * $y) + 0.00888) * $y) + 0.00325) * $y) + 365241.62603) * $y) + 2451716.56767;
  1531. } else if ($season == 'AUTUMNALEQUINOX') {
  1532. $juliandate = ((((((( 0.00078 * $y) + 0.00337) * $y) - 0.11575) * $y) + 365242.01767) * $y) + 2451810.21715;
  1533. } else if ($season == 'WINTERSOLSTICE') {
  1534. $juliandate = ((((((( 0.00032 * $y) - 0.00823) * $y) - 0.06223) * $y) + 365242.74049) * $y) + 2451900.05952;
  1535. }
  1536. }
  1537. return ($juliandate);
  1538. } // end func dateSeason
  1539. } // end class Date_Calc
  1540. ?>