1 |
dashley |
35 |
<?php
|
2 |
|
|
//$Header: /hl/cvsroots/gpl01/gpl01/webprojs/fboprime/sw/phplib/dt8.inc,v 1.2 2006/04/14 07:54:49 dashley Exp $
|
3 |
|
|
//--------------------------------------------------------------------------------------------------------------
|
4 |
|
|
//dt8.inc--FboPrime Functions Manipulating DT8 Data Type
|
5 |
|
|
//Copyright (C) 2006 David T. Ashley
|
6 |
|
|
//
|
7 |
|
|
//This program is free software; you can redistribute it and/or
|
8 |
|
|
//modify it under the terms of the GNU General Public License
|
9 |
|
|
//as published by the Free Software Foundation; either version 2
|
10 |
|
|
//of the License, or (at your option) any later version.
|
11 |
|
|
//
|
12 |
|
|
//This program is distributed in the hope that it will be useful,
|
13 |
|
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
|
|
//GNU General Public License for more details.
|
16 |
|
|
//
|
17 |
|
|
//You should have received a copy of the GNU General Public License
|
18 |
|
|
//along with this program; if not, write to the Free Software
|
19 |
|
|
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
20 |
|
|
//********************************************************************************
|
21 |
|
|
//This contains functions that deal with the DT8 data type (calenadar dates in
|
22 |
|
|
//the form "YYYYMMDD", for example "20060415".
|
23 |
|
|
//--------------------------------------------------------------------------------------------------------------
|
24 |
|
|
require_once("config.inc");
|
25 |
|
|
require_once("confighard.inc");
|
26 |
|
|
require_once("datefunc.inc");
|
27 |
|
|
//--------------------------------------------------------------------------------------------------------------
|
28 |
|
|
//
|
29 |
|
|
//Returns (1) if the DT8 is valid, or (0) otherwise. Additionally, if valid, the components
|
30 |
|
|
//are returned as integers, otherwise the page load date is returned. Here, "valid" is defined as:
|
31 |
|
|
// a)Right type.
|
32 |
|
|
// b)Syntactically valid.
|
33 |
|
|
// c)Date is within minimums and maximums of calendaring logic.
|
34 |
|
|
// d)Date is consistent with number of months in year and number of days in affected month.
|
35 |
|
|
//
|
36 |
|
|
//Parameters:
|
37 |
|
|
// dt8 : Value to examine.
|
38 |
|
|
// valid : (Output) 1 if valid, 0 otherwise.
|
39 |
|
|
// components : (Output) Defined only if valid,
|
40 |
|
|
// [0] Year, integer.
|
41 |
|
|
// [1] Month
|
42 |
|
|
// [2] Day
|
43 |
|
|
//
|
44 |
|
|
function DT8_extract_components($dt8, &$valid, &$components)
|
45 |
|
|
{
|
46 |
|
|
global $GLOBAL_stime_year;
|
47 |
|
|
global $GLOBAL_stime_month;
|
48 |
|
|
global $GLOBAL_stime_day;
|
49 |
|
|
|
50 |
|
|
//Define the output array to be today's date, just in case the parse fails.
|
51 |
|
|
$components = array ($GLOBAL_stime_year, $GLOBAL_stime_month, $GLOBAL_stime_day);
|
52 |
|
|
|
53 |
|
|
//Similarly, define $valid to be 0 so that a return creates an invalid flag.
|
54 |
|
|
$valid = 0;
|
55 |
|
|
|
56 |
|
|
if (!is_string($dt8)) //Must be right type.
|
57 |
|
|
return;
|
58 |
|
|
|
59 |
|
|
if (strlen($dt8) != 8) //Must be right length.
|
60 |
|
|
return;
|
61 |
|
|
|
62 |
|
|
//Extract characters.
|
63 |
|
|
$c0 = SubStr($dt8, 0, 1);
|
64 |
|
|
$c1 = SubStr($dt8, 1, 1);
|
65 |
|
|
$c2 = SubStr($dt8, 2, 1);
|
66 |
|
|
$c3 = SubStr($dt8, 3, 1);
|
67 |
|
|
$c4 = SubStr($dt8, 4, 1);
|
68 |
|
|
$c5 = SubStr($dt8, 5, 1);
|
69 |
|
|
$c6 = SubStr($dt8, 6, 1);
|
70 |
|
|
$c7 = SubStr($dt8, 7, 1);
|
71 |
|
|
|
72 |
|
|
//Check the character sets. Conditions below are necessary but not sufficient.
|
73 |
|
|
//
|
74 |
|
|
if (strpos("2" , $c0) === FALSE) //Thousands of year must be 2.
|
75 |
|
|
return;
|
76 |
|
|
if (strpos("0" , $c1) === FALSE) //Hundreds of year must be 0.
|
77 |
|
|
return;
|
78 |
|
|
if (strpos("0123456789" , $c2) === FALSE) //Tens of year may be any digit.
|
79 |
|
|
return;
|
80 |
|
|
if (strpos("0123456789" , $c3) === FALSE) //Ones of year may be any digit.
|
81 |
|
|
return;
|
82 |
|
|
if (strpos("01" , $c4) === FALSE) //Tens of month must be 0 or 1.
|
83 |
|
|
return;
|
84 |
|
|
if (strpos("0123456789" , $c5) === FALSE) //Ones of month may be any digit.
|
85 |
|
|
return;
|
86 |
|
|
if (strpos("0123" , $c6) === FALSE) //Tens of day must be 0-3.
|
87 |
|
|
return;
|
88 |
|
|
if (strpos("0123456789" , $c7) === FALSE) //Ones of day may be any digit.
|
89 |
|
|
return;
|
90 |
|
|
|
91 |
|
|
//Grab the values.
|
92 |
|
|
$val0 = strpos("0123456789", $c0);
|
93 |
|
|
$val1 = strpos("0123456789", $c1);
|
94 |
|
|
$val2 = strpos("0123456789", $c2);
|
95 |
|
|
$val3 = strpos("0123456789", $c3);
|
96 |
|
|
$val4 = strpos("0123456789", $c4);
|
97 |
|
|
$val5 = strpos("0123456789", $c5);
|
98 |
|
|
$val6 = strpos("0123456789", $c6);
|
99 |
|
|
$val7 = strpos("0123456789", $c7);
|
100 |
|
|
|
101 |
|
|
//Calculate the integers representing year, month, and day.
|
102 |
|
|
$year_int = $val0 * 1000 + $val1 * 100 + $val2 * 10 + $val3;
|
103 |
|
|
$month_int = $val4 * 10 + $val5;
|
104 |
|
|
$day_int = $val6 * 10 + $val7;
|
105 |
|
|
|
106 |
|
|
//Confine year.
|
107 |
|
|
if (($year_int < CONFIGHARD_DATEFUNC_MINYEAR) || ($year_int >= CONFIGHARD_DATEFUNC_MAXYEAR))
|
108 |
|
|
return;
|
109 |
|
|
|
110 |
|
|
//Confine month.
|
111 |
|
|
if (($month_int < 1) || ($month_int > 12))
|
112 |
|
|
return;
|
113 |
|
|
|
114 |
|
|
//Figure out how many days are supposed to be in that month, anyway.
|
115 |
|
|
$ndays = DATEFUNC_year_month_days($year_int, $month_int);
|
116 |
|
|
|
117 |
|
|
//Confine days.
|
118 |
|
|
if (($day_int < 1) || ($day_int > $ndays))
|
119 |
|
|
return;
|
120 |
|
|
|
121 |
|
|
//We have a confirmed good date. Return the info.
|
122 |
|
|
$valid = 1;
|
123 |
|
|
$components[0] = $year_int;
|
124 |
|
|
$components[1] = $month_int;
|
125 |
|
|
$components[2] = $day_int;
|
126 |
|
|
}
|
127 |
|
|
//
|
128 |
|
|
//--------------------------------------------------------------------------------------------------------------
|
129 |
|
|
//
|
130 |
|
|
//Makes a DT8 from components of integer year, integer month, and integer day.
|
131 |
|
|
//
|
132 |
|
|
function DT8_make_from_ints($year, $month, $day)
|
133 |
|
|
{
|
134 |
|
|
return(sprintf("%04d", $year) . sprintf("%02d", $month) . sprintf("%02d", $day));
|
135 |
|
|
}
|
136 |
|
|
//
|
137 |
|
|
//--------------------------------------------------------------------------------------------------------------
|
138 |
|
|
//End of $RCSfile: dt8.inc,v $.
|
139 |
|
|
//--------------------------------------------------------------------------------------------------------------
|
140 |
|
|
?>
|