Source for file datetime-defs.php

Documentation is available at datetime-defs.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: datetime-defs.php */
  22. /* Author: Paul Waite */
  23. /* Description: Various general date/time routines. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package datetime *//** Example: 31/12/1999 23:59 */
  27. ("DISPLAY_DATE_FORMAT", "d/m/Y H:i");
  28.  
  29. /** Example: 31/12/1999 23:59:59 */
  30. ("DISPLAY_TIMESTAMP_FORMAT", "d/m/Y H:i:s");
  31.  
  32. /** Example: 31/12/1999 */
  33. ("DISPLAY_DATE_ONLY", "d/m/Y");
  34.  
  35. /** Example: 1999/12/31 */
  36. ("BACKWARDS_DATE_ONLY", "Y/m/d");
  37.  
  38. /** Example: Mar 3rd */
  39. ("NICE_DATE_NOYEAR", "M jS");
  40.  
  41. /** Example: Mar 3rd 1:30pm */
  42. ("NICE_DATETIME", "M jS g:ia");
  43.  
  44. /** Example: Mar 3 21:30 */
  45. ("SHORT_DATETIME", "M j H:i");
  46.  
  47. /** Example: Mar 3rd 1999 */
  48. ("NICE_DATE", "M jS Y");
  49.  
  50. /** Example: Mar 3rd 1999 1:30pm */
  51. ("NICE_FULLDATETIME", "M jS Y g:ia");
  52.  
  53. /** Example: Friday, 20th July 2001 */
  54. ("DAY_AND_DATE", "l, jS F Y");
  55.  
  56. /** Example: 23:59 */
  57. ("DISPLAY_TIME_ONLY", "H:i");
  58.  
  59. /** Example: 1:30pm */
  60. ("NICE_TIME_ONLY", "g:ia");
  61.  
  62. /** Example: 12-31-1999 23:59:59 */
  63. ("POSTGRES_DATE_FORMAT", "m/d/Y H:i:s");
  64.  
  65. /** Example: 1999-07-17 23:59:59 */
  66. ("POSTGRES_STD_FORMAT", "Y-m-d H:i:s");
  67.  
  68. /** Example: 1999-07-17 23:59:59 */
  69. ("ISO_DATABASE_FORMAT", "Y-m-d H:i:s");
  70.  
  71. /** Example: 23/5/2001 */
  72. ("STD_DMY", "d/m/Y");
  73.  
  74. /** Example: 12/31/1999 23:59:59 */
  75. ("SQL_FORMAT", "m/d/Y H:i:s");
  76.  
  77. /** Example: Sun 23:59 */
  78. ("DOW_HHMM", "D H:i");
  79.  
  80. /** ISO 8601 Example: YYYYMMDDTHHMMSS-HHMM */
  81. ("ISO_8601", "Ymd\THisO");
  82.  
  83. // -----------------------------------------------------------------------
  84. // Handy vars..
  85.  
  86. /** Months associative array keyed on three-letter monthname, returns monthno (1-12) */
  87. = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,
  88. "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8,
  89. "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12);
  90. /** Array elements 1-12 contain full month names, zeroth element is 'Unknown' */
  91. = array("Unknown","January","February","March","April","May","June",
  92. "July","August","September","October","November","December");
  93. /** Array of month lengths 0-11 */
  94. = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  95. /** Days of week keyed by three-letter day-name, returns day number (0-6) */
  96. = array("Sun" => 0, "Mon" => 1, "Tue" => 2, "Wed" => 3, "Thu" => 4,
  97. "Fri" => 5, "Sat" => 6);
  98. /** Array elements 0-6 contain full day names Sunday(0) -> Saturday(6) */
  99. = array("Sunday","Monday","Tuesday","Wednesday","Thursday",
  100. "Friday","Saturday");
  101.  
  102. // .......................................................................
  103. /**
  104. * Return the days in a month
  105. * Given an integer month number 1-12, and an optional
  106. * year (defaults to current) this function returns
  107. * the number of days in the given month.
  108. * @param integer $monthno Month no. 1 - 12
  109. * @param integer $year Year
  110. * @return integer Number of days in month
  111. */
  112. function daysinmonth($monthno, $year=0) {
  113. global $monthlengths;
  114. $numdays = 0;
  115. if ($monthno >= 1 && $monthno <= 12) {
  116. $numdays = $monthlengths[$monthno];
  117. // The leap year crappola..
  118. if ($monthno == 2) {
  119. if ($year == 0) {
  120. $datetoday = getdate();
  121. $year = $datetoday["year"];
  122. }
  123. if (isLeapYear($year)) {
  124. $numdays += 1;
  125. }
  126. }
  127. }
  128. return $numdays;
  129. }
  130.  
  131. // .......................................................................
  132. /**
  133. * Check if year is a leap year.
  134. * Determines whether the given year is a leap year.
  135. * @param integer $year Year
  136. * @return boolean True if year is a leap year, else false
  137. */
  138. function isLeapYear($year) {
  139. $result = ($year % 4 == 0) &&
  140. (($year % 100 != 0) || ($year % 400 == 0));
  141. return $result;
  142. }
  143.  
  144. // .......................................................................
  145. /**
  146. * Tell nice difference between too datetimes
  147. * Returns a nice expression of the difference between the
  148. * two given datetimes written in a nice English sentence
  149. * @param string $dfrom From date in Postgres datetime format
  150. * @param string $dto To date in Postgres datetime format
  151. * @return string Nice sentence describing difference
  152. */
  153. function prose_diff($dfrom, $dto) {
  154. $prose = "";
  155. if ($dfrom == "now") $fromts = time();
  156. else $fromts = datetime_to_timestamp($dfrom);
  157.  
  158. if ($dto == "now") $tots = time();
  159. else $tots = datetime_to_timestamp($dto);
  160.  
  161. return prose_diff_ts($fromts, $tots);
  162. } // prose_diff
  163. // .......................................................................
  164.  
  165. /**
  166. * Tell nice difference between too Unix timestamps
  167. * Returns a nice expression of the difference between the
  168. * two given timestamps, written in a nice English sentence
  169. * @param integer $fromts From datetime as Unix timestamp
  170. * @param integer $tots To datetime as Unix timestamp
  171. * @return string Nice sentence describing the time interval
  172. */
  173. function prose_diff_ts($fromts, $tots) {
  174. $prose = "";
  175. $diff = $tots - $fromts;
  176. if ($diff >= 0) {
  177. $weeks = floor($diff / 604800);
  178. $diff = $diff - ($weeks * 604800);
  179. $days = floor($diff / 86400);
  180. $diff = $diff - ($days * 86400);
  181. $hours = floor($diff / 3600);
  182. $diff = $diff - ($hours * 3600);
  183. $mins = floor($diff / 60);
  184. if ($weeks > 0) $prose .= "$weeks weeks";
  185. if ($days > 0) {
  186. if ($prose != "") $prose .= ", ";
  187. $prose .= "$days days";
  188. }
  189. if ($hours > 0) {
  190. if ($prose != "") $prose .= ", ";
  191. $prose .= "$hours hours";
  192. }
  193. if ($mins > 0) {
  194. if ($prose != "") $prose .= ", and ";
  195. $prose .= "$mins minutes";
  196. }
  197. }
  198. return $prose;
  199. } // prose_diff_ts
  200. // .......................................................................
  201.  
  202. /**
  203. * Tell minutes difference between too datetimes
  204. * Returns the minutes difference between the two given
  205. * datetimes.
  206. * @param string $dfrom From date in 'descriptive' format
  207. * @param string $dto To date in 'descriptive' format
  208. * @return integer The number of minutes difference
  209. */
  210. function mins_diff($dfrom, $dto) {
  211. if ($dfrom == "now") $fromts = time();
  212. else $fromts = datetime_to_timestamp($dfrom);
  213.  
  214. if ($dto == "now") $tots = time();
  215. else $tots = datetime_to_timestamp($dto);
  216.  
  217. $diff = $tots - $fromts;
  218. if ($diff >= 0) $mins = floor($diff / 60);
  219. else $mins = -1;
  220.  
  221. return $mins;
  222. }
  223.  
  224. // -----------------------------------------------------------------------
  225. // DATE CONVERSION: USER DISPLAY -> DATABASE
  226. // -----------------------------------------------------------------------
  227.  
  228. /**
  229. * Conversion: descriptive to timestamp.
  230. * Converts a descriptive date/time string into a timestamp.
  231. * @param string $displaydate Date in any 'descriptive' format
  232. * @return integer Unix timestamp (earliest = 1970-1-1)
  233. */
  234. function displaydate_to_timestamp($displaydate) {
  235. $res = 0;
  236. if ($displaydate) {
  237. $dmy = get_DMY($displaydate);
  238. if (($dmy["year"] == 1900 || $dmy["year"] == 1970) && $dmy["month"] == 1 && $dmy["day"] == 1) {
  239. // Invalid date, so try magic wand..
  240. $res = strtotime($displaydate);
  241. }
  242. else {
  243. if (checkdate($dmy["month"], $dmy["day"], $dmy["year"])) {
  244. $hms = get_HMS($displaydate);
  245. $res = mktime( $hms["hours"], $hms["minutes"], $hms["seconds"],
  246. $dmy["month"], $dmy["day"], $dmy["year"] );
  247. }
  248. else {
  249. // Try this magic routine as last resort..
  250. $res = strtotime($displaydate);
  251. }
  252. }
  253. }
  254. return $res;
  255. }
  256.  
  257. // .......................................................................
  258. /**
  259. * Conversion: timestamp to datetime.
  260. * Returns a string in the correct format for populating
  261. * a database 'datetime' field, as per ISO 8601 spec.
  262. * @param integer $timestamp Unix timestamp
  263. * @return string Datetime string in ISO YYYY-MM-DD HH:MM:SS format
  264. */
  265. function timestamp_to_datetime($timestamp=false) {
  266. $res = false;
  267. if ($timestamp === false) {
  268. $timestamp = time();
  269. }
  270. $res = date(ISO_DATABASE_FORMAT, $timestamp);
  271. return $res;
  272. }
  273.  
  274. // .......................................................................
  275. /**
  276. * Conversion: descriptive to datetime.
  277. * Converts a descriptive date/time string into a database
  278. * compatible 'datetime' field as per Postgres ISO spec.
  279. * @param string $displaydate Date in any 'descriptive' format
  280. * @return string Datetime string in Postgres YYYY-MM-DD HH:MM:SS format
  281. */
  282. function displaydate_to_datetime($displaydate) {
  283. return timestamp_to_datetime( displaydate_to_timestamp($displaydate) );
  284. }
  285.  
  286. // .......................................................................
  287. /**
  288. * Conversion: descriptive to datetime without the time element.
  289. * Converts a descriptive date/time string into a database
  290. * compatible 'date' field as per Postgres ISO spec.
  291. * @param string $displaydate Date in any 'descriptive' format
  292. * @return string Date string in Postgres YYYY-MM-DD format
  293. */
  294. function displaydate_to_date($displaydate) {
  295. $dt = displaydate_to_datetime($displaydate);
  296. $bits = explode(" ", $dt);
  297. return $bits[0];
  298. }
  299.  
  300. // -----------------------------------------------------------------------
  301. // DATE CONVERSION: DATABASE -> USER DISPLAY
  302. // -----------------------------------------------------------------------
  303.  
  304. /**
  305. * Conversion: datetime to descriptive
  306. * Returns a timestamp from a database-formatted datetime string.
  307. * We are assuming the datetime is formatted in the ISO format of
  308. * "1999-07-17 23:59:59+12" - POSTGRES_STD_FORMAT (ISO)
  309. * This is set up by the standard application via an SQL query
  310. * with the "SET DATESTYLE ISO" command (see query-defs.php).
  311. * @param string $datetime Database-compatible ISO date string
  312. * @return integer Unix timestamp (earliest = 1970-1-1)
  313. */
  314. function datetime_to_timestamp($datetime) {
  315. $res = FALSE;
  316. if ($datetime) {
  317. $dt = explode("+", $datetime);
  318. $dtim = explode(" ", $dt[0]);
  319.  
  320. $ymd = explode("-", $dtim[0]);
  321. if (isset($dtim[1])) $tim = explode(":", $dtim[1]);
  322.  
  323. if (isset($ymd[0])) $year = $ymd[0];
  324. else $year = 1970;
  325. if (isset($ymd[1])) $month = $ymd[1];
  326. else $month = 1;
  327. if (isset($ymd[2])) $day = $ymd[2];
  328. else $day = 1;
  329.  
  330. if (isset($tim[0])) $hrs = $tim[0];
  331. else $hrs = 0;
  332. if (isset($tim[1])) $min = $tim[1];
  333. else $min = 0;
  334. if (isset($tim[2])) $sec = $tim[2];
  335. else $sec = 0;
  336.  
  337. $res = mktime($hrs, $min, $sec, $month, $day, $year);
  338. }
  339. return $res;
  340. }
  341.  
  342. // .......................................................................
  343. /**
  344. * Conversion: timestamp to descriptive
  345. * Convert a Unix timestamp into a descriptive date/time format
  346. * for the user display, using the given displayformat string.
  347. * @param string $displayformat See defined formats.
  348. * @param integer $timestamp Unix timestamp
  349. * @return string The formatted descriptive datetime string
  350. */
  351. function timestamp_to_displaydate($displayformat, $timestamp="") {
  352. if ($timestamp == "") {
  353. $timestamp = time();
  354. }
  355. return date($displayformat, $timestamp);
  356. }
  357.  
  358. // .......................................................................
  359. /**
  360. * Conversion: datetime to descriptive
  361. * Convert a database-compatible datetime string into a
  362. * descriptive date/time format for the user display,
  363. * using the given displayformat string.
  364. * @param string $displayformat See defined formats.
  365. * @param string $datetime Database datetime string
  366. * @return string The formatted descriptive datetime string
  367. */
  368. function datetime_to_displaydate($displayformat, $datetime) {
  369. if ($datetime == NULL) {
  370. return "";
  371. }
  372. else {
  373. return timestamp_to_displaydate( $displayformat, datetime_to_timestamp($datetime) );
  374. }
  375. }
  376.  
  377. // .......................................................................
  378. /**
  379. * Format date as DD/MM/YYYY
  380. * Take a date string as entered in a form and reformat it
  381. * to DD/MM/YYYY. Obviously it as to be more or less in this
  382. * format to begin with, but we cope with a few foibles here
  383. * like two-digit year, different delimiters and also strip
  384. * off any time component added to the end.
  385. * @param string $dtstr Date string to format
  386. * @return string The formatted DD/MM/YYYY date
  387. */
  388. function format_DMY($dtstr="") {
  389. // Null parm means grab todays date...
  390. if ($dtstr == "") return todaysDate_DMY();
  391. list($day, $month, $year) = get_DMY($dtstr);
  392.  
  393. // Return the date with leading zeros..
  394. return (sprintf("%02d/%02d/%04d", $day, $month, $year));
  395. }
  396.  
  397. // .......................................................................
  398. /**
  399. * Return month number
  400. * Return the number of the named month, default
  401. * to Jan if problems arise.
  402. * @param string $monthname Name of the month
  403. * @return integer The number of the month 1 - 12
  404. */
  405. function monthno($monthname) {
  406. global $months;
  407. $res = 1;
  408. if ($monthname) {
  409. $monthname = substr(ucfirst(strtolower(trim($monthname))), 0, 3);
  410. $res = $months["$monthname"];
  411. }
  412. return $res;
  413. }
  414.  
  415. // .......................................................................
  416. /**
  417. * Today as MM/DD/YYYY
  418. * Returns the given datetime as a date, and in SQL
  419. * 'm/d/Y' format.
  420. * @return string The formatted MM/DD/YYYY SQL date
  421. */
  422. function todaysDate_MDY() {
  423. return date("m/d/Y");
  424. }
  425.  
  426. // .......................................................................
  427. /**
  428. * Today as DD/MM/YYYY
  429. * Returns the given datetime as a date, and in EUROPEAN
  430. * 'd/m/Y' format.
  431. * @return string The formatted DD/MM/YYYY date
  432. */
  433. function todaysDate_DMY() {
  434. return date("d/m/Y");
  435. }
  436.  
  437. // .......................................................................
  438. /**
  439. * Time now as array
  440. * Returns the current time as an associative array:
  441. * ["hours"] - Hours since midnight
  442. * ["minutes"] - Minute of the curent hour
  443. * ["seconds"] - Seconds of the current minute
  444. * @return array Containing the time components
  445. */
  446. function timeNow() {
  447. $tod = getdate();
  448. $tinow["hours"] = $tod["hours"];
  449. $tinow["minutes"] = $tod["minutes"];
  450. $tinow["seconds"] = $tod["seconds"];
  451. return $tinow;
  452. }
  453.  
  454. // .......................................................................
  455. /**
  456. * Time now as string
  457. * Returns the current time as a string 'hr:min:sec'
  458. * @return string Time now in hh:mm:ss format.
  459. */
  460. function timeNowAsStr() {
  461. $tinow = timeNow();
  462. return $tinow["hours"] . ":" . $tinow["minutes"] . ":" . $tinow["seconds"];
  463. }
  464.  
  465. // .......................................................................
  466. /**
  467. * Year now as integer
  468. * Returns the year as it is now.
  469. * @return integer Year now in 9999 format
  470. */
  471. function yearNow() {
  472. $tod = getdate();
  473. return $tod["year"];
  474. }
  475.  
  476. // .......................................................................
  477. /**
  478. * Month now as integer
  479. * Returns the month (1-12) as it is now.
  480. * @return integer Month number now
  481. */
  482. function monthNow() {
  483. $tod = getdate();
  484. return $tod["mon"];
  485. }
  486.  
  487. // .......................................................................
  488. /**
  489. * Month now as string
  490. * Returns the month name (eg. "January") as it is now..
  491. * @return string Month name now
  492. */
  493. function monthNameNow() {
  494. $tod = getdate();
  495. return $tod["month"];
  496. }
  497.  
  498. // .......................................................................
  499. /**
  500. * Day of Month now as integer
  501. * Returns the day of the month (1-31) as it is now.
  502. * @return integer Day of Month now
  503. */
  504. function dayNow() {
  505. $tod = getdate();
  506. return $tod["mday"];
  507. }
  508.  
  509. // .......................................................................
  510. /**
  511. * Day of Week now as string
  512. * Returns the day name (eg. "Monday") as it is now.
  513. * @return string Day of week name
  514. */
  515. function dayNameNow() {
  516. $tod = getdate();
  517. return $tod["weekday"];
  518. }
  519.  
  520. // .......................................................................
  521. /**
  522. * Get date components of datetime string
  523. * Returns the date components of a formatted date string.
  524. * We start by assuming DD/MM/YYYY ordering, but can
  525. * alter this if it looks wrong. Note that the date string we
  526. * are looking at must be in vaguely standard format, ie. with
  527. * "/" or "-" or "." as delimiter etc. We return an array as:
  528. * ["day"] - Day of month (1 - 31)
  529. * ["month"] - Month number (1 - 12)
  530. * ["year"] - Year eg. 1982
  531. * @param string $displaydate Descriptive display date string
  532. * @return array Date components
  533. */
  534. function get_DMY($displaydate) {
  535. $dtbits = explode(" ", $displaydate);
  536. $dt = $dtbits[0];
  537.  
  538. // We have to have a valid delimiter..
  539. if (strstr($dt, "/")) $delim = "/";
  540. elseif (strstr($dt, "-")) $delim = "-";
  541. elseif (strstr($dt, ".")) $delim = ".";
  542. else $delim = "";
  543.  
  544. // No delimiter means no date..
  545. if ($delim == "") {
  546. $day = 1;
  547. $month = 1;
  548. $year = 1900;
  549. }
  550. else {
  551. $dtbits = explode($delim, $dt);
  552. $day = $dtbits[0];
  553. $month = $dtbits[1];
  554. $year = $dtbits[2];
  555. }
  556.  
  557. // Check for year-month-day order..
  558. if ($day > 31) {
  559. $tmp = $year;
  560. $year = $day;
  561. $day = $tmp;
  562. }
  563.  
  564. // Check for month-day-year order..
  565. if ($month > 12 && $day <= 12) {
  566. $tmp = $month;
  567. $month = $day;
  568. $day = $tmp;
  569. }
  570.  
  571. // Make sure year is always four digits..
  572. if ($year < 30) $year += 2000;
  573. elseif ($year < 100) $year += 1900;
  574.  
  575. // Final validity check..
  576. if (!checkdate($month, $day, $year)) {
  577. $day = 1;
  578. $month = 1;
  579. $year = 1900;
  580. }
  581.  
  582. // Return as a assoc. array
  583. $dmy["day"] = $day;
  584. $dmy["month"] = $month;
  585. $dmy["year"] = $year;
  586. return $dmy;
  587. }
  588.  
  589. // .......................................................................
  590. /**
  591. * Get time components of datetime string
  592. * Extract the HH:MM[:SS] from datetime string
  593. * Returns the time components of a formatted date string.
  594. * We return an array as:
  595. * ["hours"] - Hours (0 - 23)
  596. * ["minutes"] - Minutes (0 - 59)
  597. * ["seconds"] - Seconds (0 - 59)
  598. * @param string $displaydate Descriptive display date string
  599. * @return array Time components
  600. */
  601. function get_HMS($displaydate="") {
  602. if ($displaydate == "") {
  603. // Return current time..
  604. return timeNow();
  605. }
  606. else {
  607. // Unpack string..
  608. if (strstr($displaydate, " ")) {
  609. $dtbits = explode(" ", $displaydate);
  610. if (isset($dtbits[1])) $ti = $dtbits[1];
  611. else $ti = $dtbits[0];
  612. }
  613. else {
  614. $ti = $displaydate;
  615. }
  616.  
  617. // Search for our time delimiter..
  618. if (strstr($ti, ":")) $delim = ":";
  619. else $delim = "";
  620.  
  621. // Only proceed if we have a delimiter. No delimiter
  622. // means there is no time in the string at all..
  623. if ($delim != "") $tibits = explode($delim, $ti);
  624.  
  625. // Extract results or default the values..
  626. if (isset($tibits[0])) $hrs = $tibits[0];
  627. else $hrs = 0;
  628. if (isset($tibits[1])) $min = $tibits[1];
  629. else $min = 0;
  630. if (isset($tibits[2])) $sec = $tibits[2];
  631. else $sec = 0;
  632. }
  633.  
  634. // Return as a assoc. array
  635. $hms["hours"] = $hrs;
  636. $hms["minutes"] = $min;
  637. $hms["seconds"] = $sec;
  638. return $hms;
  639. }
  640.  
  641. // .......................................................................
  642. /** Classes to handle a 24-hr time schedule. Covers the setting of
  643. ** any number of slots, and testing whether a given time is in one of
  644. ** the slots. The timeslot class. Holds details of a single slot.
  645. * @package datetime
  646. */
  647. class timeslot {
  648. var $ts_start = 0;
  649. var $ts_end = 0;
  650. var $name = "";
  651. function timeslot($start=0, $end=0, $name="") {
  652. $this->ts_start = $start;
  653. $this->ts_end = $end;
  654. $this->name = $name;
  655. }
  656. function inslot($time) {
  657. return ($time >= $this->ts_start && $time <= $this->ts_end);
  658. }
  659. }
  660.  
  661. // .......................................................................
  662. /** Classes to handle a 24-hr time schedule. Covers the setting of
  663. ** any number of slots, and testing whether a given time is in one of
  664. ** the slots. The schedule class. Holds multiple timeslots.
  665. * @package datetime
  666. */
  667. class schedule {
  668. /** Array of timeslots in a schedule. */
  669.  
  670. var $timeslots = array();
  671.  
  672. function schedule($timeslots="") {
  673. if (is_array($timeslots)) {
  674. $this->timeslots = $timeslots;
  675. }
  676. }
  677. // .....................................................................
  678. /**
  679. * Add timeslot. The start and end parameters can either be Unix timestamps,
  680. * or string datetime specifiers.
  681. * @param mixed $start Start time for timeslot, string datetime or Unix timestamp
  682. * @param mixed $end End time for timeslot, string datetime or Unix timestamp
  683. * @param string $name The name or ID associated with this timeslot.
  684. */
  685. function add_timeslot($start, $end, $name="") {
  686. if (!is_int($start)) {
  687. $start = strtotime($start);
  688. }
  689. if (!is_int($end)) {
  690. $end = strtotime($end);
  691. }
  692. $this->timeslots[] = new timeslot($start, $end, $name);
  693. }
  694. // .....................................................................
  695. /*
  696. * Check if the given time lies within a schedule slot. If it isn't then we
  697. * return false. If it is then we return the name of the slot that it is in.
  698. * If timeslots overlap then the rule is we return the FIRST one which we
  699. * find. That way you should start by defining short/specific timeslots and
  700. * add the generic/catchall timeslots at the bottom of your list.
  701. * @param mixed $datetime Time to check, string datetime or Unix timestamp
  702. */
  703. function timeslot($datetime) {
  704. $slot = false;
  705. if (!is_int($datetime)) {
  706. $datetime = strtotime($datetime);
  707. }
  708. foreach ($this->timeslots as $timeslot) {
  709. if ($timeslot->inslot($datetime)) {
  710. $slot = $timeslot->name;
  711. break;
  712. }
  713. }
  714. // Return FALSE or slotname found..
  715. return $slot;
  716. }
  717. }
  718.  
  719. // -----------------------------------------------------------------------
  720. ?>

Documentation generated by phpDocumentor 1.3.0RC3