Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- Search for |

- File List
- File Members

# Axis--Date.php

Go to the documentation of this file.

    00001 <?PHP
    00002
    00003 #
    00004 #   Axis--Date.php
    00005 #   A Date Manipulation Object
    00006 #
    00007 #   Copyright 1999-2004 Axis Data
    00008 #   This code is free software that can be used or redistributed under the
    00009 #   terms of Version 2 of the GNU General Public License, as published by the
    00010 #   Free Software Foundation (http://www.fsf.org).
    00011 #
    00012 #   Author:  Edward Almasy (almasy@axisdata.com)
    00013 #
    00014 #   Part of the AxisPHP library v1.2.5
    00015 #   For more information see http://www.axisdata.com/AxisPHP/
    00016 #
    00017
    00018 class Date {
    00019
    00020     # ---- PUBLIC INTERFACE --------------------------------------------------
    00021
    00022     # object constructor
    00023     function Date($BeginDate, $EndDate = NULL, $Precision = NULL, $DebugLevel = 0)
    00024     {
    00025         # set debug state
    00026         $this->DebugLevel = $DebugLevel;
    00027
    00028         if ($this->DebugLevel) {  print("Date:  Date(BeginDate=\"".$BeginDate."\" EndDate=\"".$EndDate."\" Precision=".$this->FormattedPrecision($Precision).")<br>\n");  }
    00029
    00030         $MonthNames = array(
    00031             "january"   => 1,
    00032             "february"  => 2,
    00033             "march"     => 3,
    00034             "april"     => 4,
    00035             "may"       => 5,
    00036             "june"      => 6,
    00037             "july"      => 7,
    00038             "august"    => 8,
    00039             "september" => 9,
    00040             "october"   => 10,
    00041             "november"  => 11,
    00042             "december"  => 12,
    00043             "jan" => 1,
    00044             "feb" => 2,
    00045             "mar" => 3,
    00046             "apr" => 4,
    00047             "may" => 5,
    00048             "jun" => 6,
    00049             "jul" => 7,
    00050             "aug" => 8,
    00051             "sep" => 9,
    00052             "oct" => 10,
    00053             "nov" => 11,
    00054             "dec" => 12
    00055             );
    00056
    00057         # Formats we need to parse:
    00058         #   1999-9-19
    00059         #   1999-9
    00060         #   9-19-1999
    00061         #   19-9-1999
    00062         #   Sep-1999
    00063         #   Sep 1999
    00064         #   Sep 9 1999
    00065         #   September 9th, 1999
    00066         #   1996,1999
    00067         #   c1999
    00068         #   1996-1999
    00069         #   9/19/01
    00070         #   9-19-01
    00071         #   199909
    00072         #   19990909
    00073         #   09-Sep-1999
    00074         #   09 Sep 1999
    00075
    00076         # append end date to begin date if available
    00077         $Date = $BeginDate;
    00078         if ($EndDate!==NULL)
    00079         {
    00080             $Date .= " - ".$EndDate;
    00081         }
    00082
    00083         # strip off any leading or trailing whitespace
    00084         $Date = trim($Date);
    00085
    00086         # bail out if we don't have anything to parse
    00087         if (strlen($Date) < 1) {  return;  }
    00088
    00089         # check for and strip out inferred indicators ("[" and "]")
    00090         $Prec = 0;
    00091         if (preg_match("/\\[/", $Date))
    00092         {
    00093             $Prec |= DATEPRE_INFERRED;
    00094             $Date = preg_replace("/[\\[\\]]/", "", $Date);
    00095         }
    00096
    00097         # check for and strip off copyright indicator (leading "c")
    00098         if (preg_match("/^c/", $Date))
    00099         {
    00100             $Prec |= DATEPRE_COPYRIGHT;
    00101             $Date = preg_replace("/^c/", "", $Date);
    00102         }
    00103
    00104         # check for and strip off continuous indicator (trailing "-")
    00105         if (preg_match("/\\-$/", $Date))
    00106         {
    00107             $Prec |= DATEPRE_CONTINUOUS;
    00108             $Date = preg_replace("/\\-$/", "", $Date);
    00109         }
    00110
    00111         # strip out any times
    00112         $Date = preg_replace("/[0-9]{1,2}:[0-9]{2,2}[:]?[0-9]{0,2}/", "", $Date);
    00113         $Date = trim($Date);
    00114
    00115         $Date = strtolower($Date);
    00116
    00117         # A regex to match short and long month names:
    00118         $MonthRegex = "(?:jan(?:uary)?|feb(?:ruary)?|mar(?:ch)?|apr(?:il)?|may".
    00119             "|jun(?:e)?|jul(?:y)?|aug(?:ust)?|sep(?:tember)?|oct(?:ober)?".
    00120             "|nov(?:ember)?|dec(?:ember)?)";
    00121
    00122         # Here we'll construct a template regex for dates
    00123         # We want a single regex that covers all the different formats
    00124         # of date we understand, with the various components of the
    00125         # date pulled out using named subexpressions (eg: (?P<name>)).
    00126         # Annoyingly, we can't re-use the same name for subexpressions
    00127         # that will never both be matched at once.
    00128         # So, we have to number them (year1, year2, etc) and figure
    00129         # out which one did match.
    00130         # Use XX_ThingNumber in the parameterized subexpressions
    00131         # (eg XX_year1).
    00132         # We'll use string substitutions later to convert the XX_ to
    00133         # begin_ and end_
    00134
    00135         $DateRegex = "(".
    00136             # Matched formats are separated by |, as this isn't used in any of the formats
    00137             # First alternative will match the following formats:
    00138             # 1999-09-19 | 19990909 | 1999-09 | 199909 | 1999
    00139             "(?:(?P<XX_year1>\d{4})(?:-?(?P<XX_month1>\d{1,2})(?:-?(?P<XX_day1>\d{1,2}))?)?)".
    00140             # Second alternative will match the following formats:
    00141             # 09-19-1999 | 19-09-1999 | 09/19/01 | 09-19-01
    00142             "|(?:(?P<XX_month2>\d{1,2})[\/-](?P<XX_day2>\d{1,2})[\/-](?P<XX_year2>(?:\d{2,4})))".
    00143             # Third alternative will match the following formats:
    00144             # 09-Sep-1999 | 09 Sep 1999 | Sep-1999 | Sep 1999
    00145             "|(?:(?:(?P<XX_day3>\d+)[ -])?(?P<XX_month3>".$MonthRegex.")[ -](?P<XX_year3>\d{4}))".
    00146             # Fourth alternative will match the following formats:
    00147             # Sep 9 1999 | September 9th, 1999
    00148             "|(?:(?P<XX_month4>".$MonthRegex.") (?P<XX_day4>\d{1,2})(?:(?:st|nd|rd|th),)? (?P<XX_year4>\d{4}))".
    00149             ")";
    00150
    00151         # If more formats are added, bump this.
    00152         $NumberOfDateRegexes = 4;
    00153
    00154         # Construct the begin and end regexes for the date range
    00155         $BeginRegex = str_replace('XX','Begin', $DateRegex );
    00156         $EndRegex   = str_replace('XX','End',   $DateRegex );
    00157
    00158         # Glue them together, making the second one optional,
    00159         # and do the matching.
    00160         if ( preg_match("/".$BeginRegex.
    00161                         "(?:(?:(?: - )|,)".$EndRegex.")?/",
    00162                         $Date, $Matches ) )
    00163         {
    00164             # Pull out the Begin and End data from the matches array:
    00165             foreach( array("Begin","End") as $Time )
    00166             {
    00167                 eval(
    00168                     # Extract the matching elements from the regex parse
    00169                     '$'.$Time.'Day   = $this->ExtractMatchData($Matches,"'.
    00170                     $Time.'_day",  $NumberOfDateRegexes );'   .
    00171                     '$'.$Time.'Month = $this->ExtractMatchData($Matches,"'.
    00172                     $Time.'_month",$NumberOfDateRegexes );'   .
    00173                     '$'.$Time.'Year  = $this->ExtractMatchData($Matches,"'.
    00174                     $Time.'_year", $NumberOfDateRegexes );'   .
    00175                     # Convert named months to month numbers:
    00176                     'if ( isset($'.$Time.'Month) && '                     .
    00177                     '     !is_numeric($'.$Time.'Month))'                  .
    00178                     '{'                                                   .
    00179                     '  $'.$Time.'Month=$MonthNames[$'.$Time.'Month];'     .
    00180                     '}'                                                   .
    00181                     # Handle 2-digit years
    00182                     'if ( isset($'.$Time.'Year) && '                      .
    00183                     '     strlen($'.$Time.'Year)==2)'                     .
    00184                     '{'                                                   .
    00185                     '  $'.$Time.'Year += ($'.$Time.'Year>50)?1900:2000;'  .
    00186                     '}'                                                   .
    00187                     # Deal with D-M-Y format, where we can
    00188                     'if ( isset($'.$Time.'Month) && $'.$Time.'Month>12)'  .
    00189                     '{'                                                   .
    00190                     '  $Tmp = $'.$Time.'Month;'                           .
    00191                     '  $'.$Time.'Month = $'.$Time.'Day;'                  .
    00192                     '  $'.$Time.'Day = $Tmp;'                             .
    00193                     '}'
    00194                     );
    00195             }
    00196         }
    00197
    00198         # use current month if begin day but no begin month specified
    00199         if (isset($BeginDay) && !isset($BeginMonth))
    00200         {
    00201             $BeginMonth = date("m");
    00202         }
    00203
    00204         # use current year if begin month but no begin year specified
    00205         if (isset($BeginMonth) && !isset($BeginYear))
    00206         {
    00207             $BeginYear = date("Y");
    00208         }
    00209
    00210         # use begin year if end month but no end year specified
    00211         if (isset($EndMonth) && !isset($EndYear))
    00212         {
    00213             $EndYear = $BeginYear;
    00214         }
    00215
    00216         # After we've shuffled around the numbers, check the result to see if
    00217         # it looks valid, dropping that which doesn't.
    00218         foreach( array("Begin","End") as $Time)
    00219         {
    00220             eval(
    00221                 # Discard invalid looking dates
    00222                 'if ( isset($'.$Time.'Year) && !($'.$Time.'Year >=1)) ' .
    00223                 '  { unset($'.$Time.'Year); } '                         .
    00224                 'if ( isset($'.$Time.'Month) && '                       .
    00225                 '     !( $'.$Time.'Month>=1 && $'.$Time.'Month<=12)) '  .
    00226                 '  { unset($'.$Time.'Month); } '                        .
    00227                 'if ( isset($'.$Time.'Day) && '                         .
    00228                 '     !( $'.$Time.'Day  >=1 && $'.$Time.'Day  <=31)) '  .
    00229                 '  { unset($'.$Time.'Day); } '
    00230                 );
    00231         }
    00232
    00233         # if no begin date found and begin date value is not illegal
    00234         if (!isset($BeginYear)
    00235                 && ($BeginDate != "0000-00-00")
    00236                 && ($BeginDate != "0000-00-00 00:00:00"))
    00237         {
    00238             # try system call to parse incoming date
    00239             $UDateStamp = strtotime($BeginDate);
    00240             if ($this->DebugLevel > 1) {  print("Date:  calling strtotime to parse BeginDate \"".$BeginDate."\" -- strtotime returned \"".$UDateStamp."\"<br>\n");  }
    00241
    00242             # if system call was able to parse date
    00243             if (($UDateStamp != -1) && ($UDateStamp !== FALSE))
    00244             {
    00245                 # set begin date to value returned by system call
    00246                 $BeginYear = date("Y", $UDateStamp);
    00247                 $BeginMonth = date("n", $UDateStamp);
    00248                 $BeginDay = date("j", $UDateStamp);
    00249             }
    00250         }
    00251
    00252         # if end date value supplied and no end date found and end date value is not illegal
    00253         if (($EndDate != NULL) && !isset($EndYear)
    00254                 && ($EndDate != "0000-00-00")
    00255                 && ($EndDate != "0000-00-00 00:00:00"))
    00256         {
    00257             # try system call to parse incoming date
    00258             $UDateStamp = strtotime($EndDate);
    00259
    00260             # if system call was able to parse date
    00261             if (($UDateStamp != -1) && ($UDateStamp !== FALSE))
    00262             {
    00263                 # set begin date to value returned by system call
    00264                 $EndYear = date("Y", $UDateStamp);
    00265                 $EndMonth = date("n", $UDateStamp);
    00266                 $EndDay = date("j", $UDateStamp);
    00267             }
    00268         }
    00269
    00270         # if end date is before begin date
    00271         if ((isset($EndYear) && isset($BeginYear) && ($EndYear < $BeginYear))
    00272             || (isset($BeginYear)  && isset($EndYear)  && ($EndYear == $BeginYear) &&
    00273                 isset($BeginMonth) && isset($EndMonth) && ($EndMonth < $BeginMonth))
    00274             || (isset($BeginYear)  && isset($EndYear)  && ($EndYear == $BeginYear) &&
    00275                 isset($BeginMonth) && isset($EndMonth) && ($EndMonth == $BeginMonth) &&
    00276                 isset($BeginDay)   && isset($EndDay)   && ($EndDay < $BeginDay)))
    00277         {
    00278             # swap begin and end dates
    00279             $TempYear = $BeginYear;
    00280             $TempMonth = $BeginMonth;
    00281             $TempDay = $BeginDay;
    00282             $BeginYear = $EndYear;
    00283             $BeginMonth = $EndMonth;
    00284             $BeginDay = $EndDay;
    00285             $EndYear = $TempYear;
    00286             $EndMonth = $TempMonth;
    00287             $EndDay = $TempDay;
    00288         }
    00289
    00290         # if precision value supplied by caller
    00291         if ($Precision != NULL)
    00292         {
    00293             # use supplied precision value
    00294             $this->Precision = $Precision;
    00295         }
    00296         else
    00297         {
    00298             # save new precision value
    00299             if (isset($BeginYear)) {  $Prec |= DATEPRE_BEGINYEAR;  }
    00300             if (isset($BeginMonth)) {  $Prec |= DATEPRE_BEGINMONTH;  }
    00301             if (isset($BeginDay)) {  $Prec |= DATEPRE_BEGINDAY;  }
    00302             if (isset($EndYear)) {  $Prec |= DATEPRE_ENDYEAR;  }
    00303             if (isset($EndMonth)) {  $Prec |= DATEPRE_ENDMONTH;  }
    00304             if (isset($EndDay)) {  $Prec |= DATEPRE_ENDDAY;  }
    00305             $this->Precision = $Prec;
    00306         }
    00307
    00308         # save new date values
    00309         if ($this->DebugLevel > 1) {  print("Date:  BeginYear = $BeginYear<br>\n");  }
    00310         if ($this->DebugLevel > 1) {  print("Date:  BeginMonth = $BeginMonth<br>\n");  }
    00311         if ($this->DebugLevel > 1) {  print("Date:  BeginDay = $BeginDay<br>\n");  }
    00312         if ($this->DebugLevel > 1) {  print("Date:  EndYear = $EndYear<br>\n");  }
    00313         if ($this->DebugLevel > 1) {  print("Date:  EndMonth = $EndMonth<br>\n");  }
    00314         if ($this->DebugLevel > 1) {  print("Date:  EndDay = $EndDay<br>\n");  }
    00315         if ($this->DebugLevel > 1) {  print("Date:  Precision = ".$this->FormattedPrecision()."<br>\n");  }
    00316         $this->BeginYear  = isset($BeginYear)  ? $BeginYear  : NULL ;
    00317         $this->BeginMonth = isset($BeginMonth) ? $BeginMonth : NULL ;
    00318         $this->BeginDay   = isset($BeginDay)   ? $BeginDay   : NULL ;
    00319         $this->EndYear    = isset($EndYear)    ? $EndYear    : NULL ;
    00320         $this->EndMonth   = isset($EndMonth)   ? $EndMonth   : NULL ;
    00321         $this->EndDay     = isset($EndDay)     ? $EndDay     : NULL ;
    00322     }
    00323
    00324     # return value suitable for display
    00325     function Formatted()
    00326     {
    00327         # if begin year available
    00328         $DateString = "";
    00329         if ($this->Precision & DATEPRE_BEGINYEAR)
    00330         {
    00331             # start with begin year
    00332             $DateString = $this->BeginYear;
    00333
    00334             # if begin month available
    00335             if ($this->Precision & DATEPRE_BEGINMONTH)
    00336             {
    00337                 # add begin month
    00338                 $DateString .= "-".$this->BeginMonth;
    00339
    00340                 # if begin day available
    00341                 if ($this->Precision & DATEPRE_BEGINDAY)
    00342                 {
    00343                     # add begin day
    00344                     $DateString .= "-".$this->BeginDay;
    00345                 }
    00346             }
    00347
    00348             # if end year available
    00349             if ($this->Precision & DATEPRE_ENDYEAR)
    00350             {
    00351                 # if separate dates
    00352                 if ($this->Precision & DATEPRE_SEPARATE)
    00353                 {
    00354                     # separate dates with comma
    00355                     $DateString .= ", ";
    00356                 }
    00357                 else
    00358                 {
    00359                     # separate dates with dash
    00360                     $DateString .= " - ";
    00361                 }
    00362
    00363                 # add end year
    00364                 $DateString .= $this->EndYear;
    00365
    00366                 # if end month available
    00367                 if ($this->Precision & DATEPRE_ENDMONTH)
    00368                 {
    00369                     # add end month
    00370                     $DateString .= "-".$this->EndMonth;
    00371
    00372                     # if end day available
    00373                     if ($this->Precision & DATEPRE_ENDDAY)
    00374                     {
    00375                         # add end day
    00376                         $DateString .= "-".$this->EndDay;
    00377                     }
    00378                 }
    00379             }
    00380             else
    00381             {
    00382                 # if date is open-ended
    00383                 if ($this->Precision & DATEPRE_CONTINUOUS)
    00384                 {
    00385                     # add dash to indicate open-ended
    00386                     $DateString .= "-";
    00387                 }
    00388             }
    00389
    00390             # if copyright flag is set
    00391             if ($this->Precision & DATEPRE_COPYRIGHT)
    00392             {
    00393                 # add on copyright indicator
    00394                 $DateString = "c".$DateString;
    00395             }
    00396
    00397             # if flag is set indicating date was inferred
    00398             if ($this->Precision & DATEPRE_INFERRED)
    00399             {
    00400                 # add on inferred indicators
    00401                 $DateString = "[".$DateString."]";
    00402             }
    00403         }
    00404
    00405         # return formatted date string to caller
    00406         return $DateString;
    00407     }
    00408
    00409     # return date in format specified like PHP date() format parameter
    00410     function PFormatted($Format, $ReturnEndDate = FALSE)
    00411     {
    00412         if ($ReturnEndDate)
    00413     {
    00414         $Month = ($this->Precision & DATEPRE_ENDMONTH) ? $this->EndMonth : 1;
    00415         $Day = ($this->Precision & DATEPRE_ENDDAY) ? $this->EndDay : 1;
    00416         $Year = ($this->Precision & DATEPRE_ENDYEAR) ? $this->EndYear : 1;
    00417     }
    00418     else
    00419     {
    00420         $Month = ($this->Precision & DATEPRE_BEGINMONTH) ? $this->BeginMonth : 1;
    00421         $Day = ($this->Precision & DATEPRE_BEGINDAY) ? $this->BeginDay : 1;
    00422         $Year = ($this->Precision & DATEPRE_BEGINYEAR) ? $this->BeginYear : 1;
    00423     }
    00424         return date($Format, mktime(0, 0, 0, $Month, $Day, $Year));
    00425     }
    00426
    00427     # get begin date/time (or end if requested) formatted for SQL DATETIME field
    00428     function FormattedForSql($ReturnEndDate = FALSE)
    00429     {
    00430         return $this->PFormatted("Y-m-d H:i:s", $ReturnEndDate);
    00431     }
    00432
    00433     # return begin time in ISO 8601 format
    00434     function FormattedISO8601()
    00435     {
    00436         # if begin year available
    00437         if ($this->Precision & DATEPRE_BEGINYEAR)
    00438         {
    00439             # start with begin year
    00440             $DateString = sprintf("%04d", $this->BeginYear);
    00441
    00442             # if begin month available
    00443             if ($this->Precision & DATEPRE_BEGINMONTH)
    00444             {
    00445                 # add begin month
    00446                 $DateString .= sprintf("-%02d", $this->BeginMonth);
    00447
    00448                 # if begin day available
    00449                 if ($this->Precision & DATEPRE_BEGINDAY)
    00450                 {
    00451                     # add begin day
    00452                     $DateString .= sprintf("-%02d", $this->BeginDay);
    00453                 }
    00454             }
    00455         }
    00456
    00457         # return ISO 8601 formatted date string to caller
    00458         return $DateString;
    00459     }
    00460
    00461     # return values in UTC instead of local time  (NOT IMPLEMENTED)
    00462     function UseUTC()
    00463     {
    00464         # if not currently in UTC
    00465         if ($this->InUTC != TRUE)
    00466         {
    00467             # adjust date to UTC
    00468             # ???
    00469
    00470             # set flag to indicate we are in UTC
    00471             $this->InUTC = TRUE;
    00472         }
    00473     }
    00474
    00475     # return values in local time instead of UTC  (NOT IMPLEMENTED)
    00476     function UseLocalTime()
    00477     {
    00478         # if currently in UTC
    00479         if ($this->InUTC)
    00480         {
    00481             # adjust date to local time
    00482             # ???
    00483
    00484             # set flag to indicate we are in local time
    00485             $this->InUTC = FALSE;
    00486         }
    00487     }
    00488
    00489     # return normalized values (suitable for storing via SQL)
    00490     function BeginDate()
    00491     {
    00492         # build date string based on current precision
    00493         if ($this->Precision & DATEPRE_BEGINYEAR)
    00494         {
    00495             if ($this->Precision & DATEPRE_BEGINMONTH)
    00496             {
    00497                 if ($this->Precision & DATEPRE_BEGINMONTH)
    00498                 {
    00499                     $DateFormat = "%04d-%02d-%02d";
    00500                 }
    00501                 else
    00502                 {
    00503                     $DateFormat = "%04d-%02d-01";
    00504                 }
    00505             }
    00506             else
    00507             {
    00508                 $DateFormat = "%04d-01-01";
    00509             }
    00510
    00511             $DateString = sprintf($DateFormat,
    00512                                   $this->BeginYear, $this->BeginMonth, $this->BeginDay);
    00513         }
    00514         else
    00515         {
    00516             $DateString = NULL;
    00517         }
    00518
    00519         # return date string to caller
    00520         return $DateString;
    00521     }
    00522     function EndDate()
    00523     {
    00524         # build date string based on current precision
    00525         if ($this->Precision & DATEPRE_ENDYEAR)
    00526         {
    00527             if ($this->Precision & DATEPRE_ENDMONTH)
    00528             {
    00529                 if ($this->Precision & DATEPRE_ENDMONTH)
    00530                 {
    00531                     $DateFormat = "%04d-%02d-%02d";
    00532                 }
    00533                 else
    00534                 {
    00535                     $DateFormat = "%04d-%02d-00";
    00536                 }
    00537             }
    00538             else
    00539             {
    00540                 $DateFormat = "%04d-00-00";
    00541             }
    00542
    00543             $DateString = sprintf($DateFormat,
    00544                                   $this->EndYear, $this->EndMonth, $this->EndDay);
    00545         }
    00546         else
    00547         {
    00548             $DateString = NULL;
    00549         }
    00550
    00551         # return date string to caller
    00552         return $DateString;
    00553     }
    00554
    00555     # get or set precision value (combination of boolean flags)
    00556     function Precision($NewPrecision = NULL)
    00557     {
    00558         if ($NewPrecision != NULL) {  $this->Precision = $NewPrecision;  }
    00559         return $this->Precision;
    00560     }
    00561
    00562     # return text of SQL condition for records that match date
    00563     function SqlCondition($FieldName, $EndFieldName = NULL, $Operator = "=")
    00564     {
    00565         # if no date value is set
    00566         if ($this->Precision < 1)
    00567         {
    00568             # if operator is equals
    00569             if ($Operator == "=")
    00570             {
    00571                 # construct conditional that will find null dates
    00572                 $Condition = "(".$FieldName." IS NULL OR ".$FieldName." < '0000-01-01 00:00:01')";
    00573             }
    00574             else
    00575             {
    00576                 # construct conditional that will find non-null dates
    00577                 $Condition = "(".$FieldName." > '0000-01-01 00:00:00')";
    00578             }
    00579         }
    00580         else
    00581         {
    00582             # use begin field name as end if no end field specified
    00583             if ($EndFieldName == NULL) {  $EndFieldName = $FieldName;  }
    00584
    00585             # determine begin and end of range
    00586             $BeginYear = $this->BeginYear;
    00587             if ($this->Precision & DATEPRE_BEGINMONTH)
    00588             {
    00589                 $BeginMonth = $this->BeginMonth;
    00590                 if ($this->Precision & DATEPRE_BEGINDAY)
    00591                 {
    00592                     $BeginDay = $this->BeginDay - 1;
    00593                 }
    00594                 else
    00595                 {
    00596                     $BeginDay = 0;
    00597                 }
    00598             }
    00599             else
    00600             {
    00601                 $BeginMonth = 1;
    00602                 $BeginDay = 0;
    00603             }
    00604             if ($this->Precision & DATEPRE_ENDYEAR)
    00605             {
    00606                 $EndYear = $this->EndYear;
    00607                 if ($this->Precision & DATEPRE_ENDMONTH)
    00608                 {
    00609                     $EndMonth = $this->EndMonth;
    00610                     if ($this->Precision & DATEPRE_ENDDAY)
    00611                     {
    00612                         $EndDay = $this->EndDay;
    00613                     }
    00614                     else
    00615                     {
    00616                         $EndMonth++;
    00617                         $EndDay = 0;
    00618                     }
    00619                 }
    00620                 else
    00621                 {
    00622                     $EndYear++;
    00623                     $EndMonth = 1;
    00624                     $EndDay = 0;
    00625                 }
    00626             }
    00627             else
    00628             {
    00629                 $EndYear = $BeginYear;
    00630                 if ($this->Precision & DATEPRE_BEGINMONTH)
    00631                 {
    00632                     $EndMonth = $BeginMonth;
    00633                     if ($this->Precision & DATEPRE_BEGINDAY)
    00634                     {
    00635                         $EndDay = $BeginDay + 1;
    00636                     }
    00637                     else
    00638                     {
    00639                         $EndMonth++;
    00640                         $EndDay = 0;
    00641                     }
    00642                 }
    00643                 else
    00644                 {
    00645                     $EndYear++;
    00646                     $EndMonth = 1;
    00647                     $EndDay = 0;
    00648                 }
    00649             }
    00650             $RangeBegin = "'".date("Y-m-d H:i:s", mktime(23, 59, 59, $BeginMonth, $BeginDay, $BeginYear))."'";
    00651             $RangeEnd = "'".date("Y-m-d H:i:s", mktime(23, 59, 59, $EndMonth, $EndDay, $EndYear))."'";
    00652
    00653             # construct SQL condition
    00654             switch ($Operator)
    00655             {
    00656                 case ">":
    00657                     $Condition = " ${FieldName} > ${RangeEnd} ";
    00658                     break;
    00659
    00660                 case ">=":
    00661                     $Condition = " ${FieldName} > ${RangeBegin} ";
    00662                     break;
    00663
    00664                 case "<":
    00665                     $Condition = " ${FieldName} <= ${RangeBegin} ";
    00666                     break;
    00667
    00668                 case "<=":
    00669                     $Condition = " ${FieldName} <= ${RangeEnd} ";
    00670                     break;
    00671
    00672                 case "!=":
    00673                     $Condition = " (${FieldName} <= ${RangeBegin}"
    00674                             ." OR ${FieldName} > ${RangeEnd}) ";
    00675                     break;
    00676
    00677                 case "=":
    00678                 default:
    00679                     $Condition = " (${FieldName} > ${RangeBegin}"
    00680                             ." AND ${FieldName} <= ${RangeEnd}) ";
    00681                     break;
    00682             }
    00683         }
    00684
    00685         # return condition to caller
    00686         return $Condition;
    00687     }
    00688
    00689     # return string containing printable version of precision flags
    00690     function FormattedPrecision($Precision = NULL)
    00691     {
    00692         if ($Precision === NULL) {  $Precision = $this->Precision;  }
    00693         $String = "";
    00694         if ($Precision & DATEPRE_BEGINYEAR) {  $String .= "| BEGINYEAR ";  }
    00695         if ($Precision & DATEPRE_BEGINMONTH) {  $String .= "| BEGINMONTH ";  }
    00696         if ($Precision & DATEPRE_BEGINDAY) {  $String .= "| BEGINDAY ";  }
    00697         if ($Precision & DATEPRE_BEGINDECADE) {  $String .= "| BEGINDECADE ";  }
    00698         if ($Precision & DATEPRE_ENDYEAR) {  $String .= "| ENDYEAR ";  }
    00699         if ($Precision & DATEPRE_ENDMONTH) {  $String .= "| ENDMONTH ";  }
    00700         if ($Precision & DATEPRE_ENDDAY) {  $String .= "| ENDDAY ";  }
    00701         if ($Precision & DATEPRE_ENDDECADE) {  $String .= "| ENDDECADE ";  }
    00702         if ($Precision & DATEPRE_INFERRED) {  $String .= "| INFERRED ";  }
    00703         if ($Precision & DATEPRE_COPYRIGHT) {  $String .= "| COPYRIGHT ";  }
    00704         if ($Precision & DATEPRE_CONTINUOUS) {  $String .= "| CONTINUOUS ";  }
    00705         if ($Precision & DATEPRE_SEPARATE) {  $String .= "| SEPARATE ";  }
    00706         $String = preg_replace("/^\\|/", "", $String);
    00707         return $String;
    00708     }
    00709
    00710
    00711     # ---- PRIVATE INTERFACE -------------------------------------------------
    00712
    00713     var $BeginDay;
    00714     var $BeginMonth;
    00715     var $BeginYear;
    00716     var $EndDay;
    00717     var $EndMonth;
    00718     var $EndYear;
    00719     var $Precision;
    00720     var $DebugLevel;
    00721
    00722     # Return the first non-empty parameterized subexpression match
    00723     # Expects a match array from preg_match()
    00724     # Expects a number of array elements, eg. match1, match2, match3
    00725     # Checks each element and returns the first non-empty one
    00726     # If they are all empty, NULL is returned
    00727     private function ExtractMatchData( $Matches, $Member, $Max )
    00728     {
    00729       for( $i=1; $i<=$Max; $i++ )
    00730       {
    00731         if (isset($Matches[$Member.$i]) && strlen($Matches[$Member.$i])>0)
    00732         {
    00733           return $Matches[$Member.$i];
    00734         }
    00735       }
    00736       return NULL;
    00737    }
    00738 }
    00739
    00740 # date precision flags
    00741 define("DATEPRE_BEGINYEAR",   1);
    00742 define("DATEPRE_BEGINMONTH",  2);
    00743 define("DATEPRE_BEGINDAY",    4);
    00744 define("DATEPRE_BEGINDECADE", 8);
    00745 define("DATEPRE_BEGINCENTURY",16);
    00746 define("DATEPRE_ENDYEAR",     32);
    00747 define("DATEPRE_ENDMONTH",    64);
    00748 define("DATEPRE_ENDDAY",      128);
    00749 define("DATEPRE_ENDDECADE",   256);
    00750 define("DATEPRE_ENDCENTURY",  512);
    00751 define("DATEPRE_INFERRED",    1024);
    00752 define("DATEPRE_COPYRIGHT",   2048);
    00753 define("DATEPRE_CONTINUOUS",  4096);
    00754 define("DATEPRE_SEPARATE",    8192);
    00755 define("DATEPRE_UNSURE",      16384);
    00756
    00757
    00758 ?>

Copyright 2009 Internet Scout