8383// Special note: in PHP, avoid declaring function inside function. That will cause "redeclaration error" when the parent function is called more than once.
8484//
8585
86+ // This function is used by getYearsByAge() and getAnimalYearsByRange()
87+ function swap (&$ item1 , &$ item2 ) {
88+ $ tmp = $ item1 ;
89+ $ item1 = $ item2 ;
90+ $ item2 = $ tmp ;
91+ }
92+
8693// This function is used by getZodiac()
8794function findIndex ($ target , $ arr , $ left =0 ) {
8895 $ max = count ($ arr )-2 ;
@@ -156,8 +163,8 @@ function getCNY($year) {
156163 214 , 202 , 122 , 210 , 130 , 217 , 207 , 127 , 215 , 203 , 123 , 211 , 201 , 219 , 208 , 129 , 217 , 205 , 125 , 213 , 202 , 122 , 210 , 130 ,
157164 218 , 207 , 127 , 215 , 204 , 123 , 211 , 201 , 220 , 208 , 129 , 216 , 205 , 125 , 212 , 202 , 123 , 210 , 130 , 218 , 207 , 126 , 214 , 203 ];
158165
159- $ index = ($ year >=1876 )?$ year -1876 :$ year ;
160- if (($ index < 0 ) || ($ index >= 288 )) {
166+ $ index = ($ year >=1876 )?$ year -1876 :$ year ; // first year is 1876
167+ if (($ index < 0 ) || ($ index >= 288 )) { // hardcode 288 for count($arr);
161168 // out of range
162169 return -1 ;
163170 }
@@ -182,10 +189,8 @@ function passedCNY($index, $month=8, $day=8) {
182189 return TRUE ;
183190 }
184191 $ cny = getCNY ($ index );
185- if ($ cny == -1 ) {
186- // out of range, default to TRUE
187- return TRUE ;
188- } else if ($ cny <= ($ month *100 + $ day )) {
192+ if ($ cny <= ($ month *100 + $ day )) {
193+ // this includes condition of $cny == -1 (out of range)
189194 return TRUE ;
190195 }
191196 return FALSE ;
@@ -223,7 +228,7 @@ function getAnimalIndex($year, $month, $day) {
223228 $ index = $ year % 12 ;
224229 } else {
225230 $ index = ($ year +11 ) % 12 ;
226- }
231+ }
227232 if ($ index < 0 ) {
228233 $ index += 12 ;
229234 }
@@ -276,8 +281,7 @@ function getYearsByAge($age1, $age2, $month=1, $day=1) {
276281 $ offset1 = $ offset2 = 0 ;
277282
278283 if ($ age1 < $ age2 ) {
279- // swap them
280- $ age1 ^= $ age2 ^= $ age1 ^= $ age2 ;
284+ swap ($ age1 ,$ age2 );
281285 }
282286 if (!inthePast ($ month ,$ day -1 )) {
283287 if ($ age1 > 0 ) {
@@ -296,8 +300,7 @@ function getYearsByAge($age1, $age2, $month=1, $day=1) {
296300function getAnimalYearsByRange ($ animalIndex , $ year1 , $ year2 , $ month =1 , $ day =1 ) {
297301// $animalIndex range from 0 to 11, according to the sequence as listed in getAnimal()
298302 if ($ year1 > $ year2 ) {
299- // swap them
300- $ year1 ^= $ year2 ^= $ year1 ^= $ year2 ;
303+ swap ($ year1 ,$ year2 );
301304 }
302305 $ result = array ();
303306 for ($ year = $ year1 ; $ year <= $ year2 ; $ year ++) {
@@ -315,7 +318,6 @@ function getAnimalYearsByRange($animalIndex, $year1, $year2, $month=1, $day=1) {
315318function getAnimalYearsByAge ($ animalIndex , $ age1 , $ age2 , $ month =1 , $ day =1 ) {
316319// $animalIndex range from 0 to 11, according to the sequence as listed in getAnimal()
317320 $ range = getYearsByAge ($ age1 ,$ age2 ,$ month ,$ day );
318-
319321 return getAnimalYearsByRange ($ animalIndex ,$ range [0 ],$ range [1 ],$ month ,$ day );
320322}
321323
0 commit comments