示例#1
0
 /**
  * Return the date as yyyymmdd for sorting
  *
  * @param string $date
  * @param boolean getNumericKey if true, returns an 8-digit numeric key that incorporates the effect of bef/aft date modifiers
  * @return string or int if getNumericKey is used
  */
 public static function getDateKey($date, $getNumericKey = false)
 {
     $result = '';
     $year = '';
     $month = '';
     $day = '';
     $monthError = $dayError = false;
     // don't set errors anymore
     $fields = array();
     // this should match 0-9+ or alphabetic(including accented)+
     preg_match_all('/(\\d+|[^0-9\\s`~!@#%^&*()_+\\-={}|:\'<>?;,\\/"\\[\\]\\.\\\\]+)/', $date, $fields, PREG_SET_ORDER);
     for ($i = 0; $i < count($fields); $i++) {
         $field = $fields[$i][1];
         $num = $field + 0;
         // force conversion to number
         if (StructuredData::isYear($num)) {
             if (!$year) {
                 $year = $num;
             }
             // take the first year and ignore later numbers
         } else {
             if ($m = StructuredData::getAlphaMonth($field)) {
                 if (!$month) {
                     $month = $m;
                 }
                 // take the first month and ignore later (in case of date ranges)
             } else {
                 if ($i > 0 && StructuredData::isYear($fields[$i - 1][1] + 0) && StructuredData::isNextYear($year, $field)) {
                     $year++;
                     // 1963/4 or 1963/64
                 } else {
                     if (StructuredData::isDay($num) && (!StructuredData::isNumMonth($num) || $i > 0 && StructuredData::getAlphaMonth($fields[$i - 1][1]) || $i < count($fields) - 1 && StructuredData::getAlphaMonth($fields[$i + 1][1]))) {
                         if (!$day) {
                             $day = $num;
                         }
                         // take the first day and ignore later
                     } else {
                         if (StructuredData::isNumMonth($num)) {
                             if (!$month) {
                                 $month = $num;
                             }
                             // take the first month and ignore later
                         }
                     }
                 }
             }
         }
     }
     if ($year) {
         $result = "{$year}";
         // force conversion back to string
         if ($month && !$monthError) {
             $result .= $month < 10 ? "0{$month}" : $month;
             if ($day && !$dayError) {
                 $result .= $day < 10 ? "0{$day}" : $day;
             } else {
                 if ($getNumericKey) {
                     $result .= "01";
                 }
             }
         } else {
             if ($getNumericKey) {
                 $result .= "0101";
             }
         }
         if ($getNumericKey) {
             $result = (int) $result;
             if (preg_match("/\\b(bef|before)\\b/i", $date)) {
                 $result -= 10000;
                 // subtract a year
             } else {
                 if (preg_match("/\\b(aft|after)\\b/i", $date)) {
                     $result += 10000;
                     // add a year
                 }
             }
         }
     }
     return $result;
 }