oxutilsdate.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxUtilsDate extends oxSuperCfg
00007 {
00008 
00017     public function formatDBDate($sDBDateIn, $blForceEnglishRet = false)
00018     {
00019         // convert english format to output format
00020         if (!$sDBDateIn) {
00021             return null;
00022         }
00023 
00024         $oStr = getStr();
00025         if ($blForceEnglishRet && $oStr->strstr($sDBDateIn, '-')) {
00026             return $sDBDateIn;
00027         }
00028 
00029         if ($this->isEmptyDate($sDBDateIn) && $sDBDateIn != '-') {
00030             return '-';
00031         } elseif ($sDBDateIn == '-') {
00032             return '0000-00-00 00:00:00';
00033         }
00034 
00035         // is it a timestamp ?
00036         if (is_numeric($sDBDateIn)) {
00037             // db timestamp : 20030322100409
00038             $sNew = substr($sDBDateIn, 0, 4) . '-' . substr($sDBDateIn, 4, 2) . '-' . substr($sDBDateIn, 6, 2) . ' ';
00039             // check if it is a timestamp or wrong data: 20030322
00040             if (strlen($sDBDateIn) > 8) {
00041                 $sNew .= substr($sDBDateIn, 8, 2) . ':' . substr($sDBDateIn, 10, 2) . ':' . substr($sDBDateIn, 12, 2);
00042             }
00043             // convert it to english format
00044             $sDBDateIn = $sNew;
00045         }
00046 
00047         // remove time as it is same in english as in german
00048         $aData = explode(' ', trim($sDBDateIn));
00049 
00050         // preparing time array
00051         $sTime = (isset($aData[1]) && $oStr->strstr($aData[1], ':')) ? $aData[1] : '';
00052         $aTime = $sTime ? explode(':', $sTime) : array(0, 0, 0);
00053 
00054         // preparing date array
00055         $sDate = isset($aData[0]) ? $aData[0] : '';
00056         $aDate = preg_split('/[\/.-]/', $sDate);
00057 
00058         // choosing format..
00059         if ($sTime) {
00060             $sFormat = $blForceEnglishRet ? 'Y-m-d H:i:s' : oxRegistry::getLang()->translateString('fullDateFormat');
00061         } else {
00062             $sFormat = $blForceEnglishRet ? 'Y-m-d' : oxRegistry::getLang()->translateString('simpleDateFormat');
00063         }
00064 
00065         if (count($aDate) != 3) {
00066             return date($sFormat);
00067         } else {
00068             return $this->_processDate($aTime, $aDate, $oStr->strstr($sDate, '.'), $sFormat);
00069         }
00070     }
00071 
00081     public function convertDBDateTime($oObject, $blToTimeStamp = false, $blOnlyDate = false)
00082     {
00083         $sDate = $oObject->value;
00084 
00085         // defining time format
00086         $sLocalDateFormat = $this->_defineAndCheckDefaultDateValues($blToTimeStamp);
00087         $sLocalTimeFormat = $this->_defineAndCheckDefaultTimeValues($blToTimeStamp);
00088 
00089         // default date/time patterns
00090         $aDefDatePatterns = $this->_defaultDatePattern();
00091 
00092         // regexps to validate input
00093         $aDatePatterns = $this->_regexp2ValidateDateInput();
00094         $aTimePatterns = $this->_regexp2ValidateTimeInput();
00095 
00096         // date/time formatting rules
00097         $aDFormats = $this->_defineDateFormattingRules();
00098         $aTFormats = $this->_defineTimeFormattingRules();
00099 
00100         // empty date field value ? setting default value
00101         if (!$sDate) {
00102             $this->_setDefaultDateTimeValue($oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
00103 
00104             return $oObject->value;
00105         }
00106 
00107         $blDefDateFound = false;
00108         $oStr = getStr();
00109 
00110         // looking for default values that are formatted by MySQL
00111         foreach (array_keys($aDefDatePatterns) as $sDefDatePattern) {
00112             if ($oStr->preg_match($sDefDatePattern, $sDate)) {
00113                 $blDefDateFound = true;
00114                 break;
00115             }
00116         }
00117 
00118         // default value is set ?
00119         if ($blDefDateFound) {
00120             $this->_setDefaultFormatedValue($oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
00121 
00122             return $oObject->value;
00123         }
00124 
00125         $blDateFound = false;
00126         $blTimeFound = false;
00127         $aDateMatches = array();
00128         $aTimeMatches = array();
00129 
00130         // looking for date field
00131         foreach ($aDatePatterns as $sPattern => $sType) {
00132             if ($oStr->preg_match($sPattern, $sDate, $aDateMatches)) {
00133                 $blDateFound = true;
00134 
00135                 // now we know the type of passed date
00136                 $sDateFormat = $aDFormats[$sLocalDateFormat][0];
00137                 $aDFields = $aDFormats[$sType][1];
00138                 break;
00139             }
00140         }
00141 
00142         // no such date field available ?
00143         if (!$blDateFound) {
00144             return $sDate;
00145         }
00146 
00147         if ($blOnlyDate) {
00148             $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
00149 
00150             return $oObject->value;
00151         }
00152 
00153         // looking for time field
00154         foreach ($aTimePatterns as $sPattern => $sType) {
00155             if ($oStr->preg_match($sPattern, $sDate, $aTimeMatches)) {
00156                 $blTimeFound = true;
00157 
00158                 // now we know the type of passed time
00159                 $sTimeFormat = $aTFormats[$sLocalTimeFormat][0];
00160                 $aTFields = $aTFormats[$sType][1];
00161 
00162                 //
00163                 if ($sType == "USA" && isset($aTimeMatches[4])) {
00164                     $iIntVal = (int) $aTimeMatches[1];
00165                     if ($aTimeMatches[4] == "PM") {
00166                         if ($iIntVal < 13) {
00167                             $iIntVal += 12;
00168                         }
00169                     } elseif ($aTimeMatches[4] == "AM" && $aTimeMatches[1] == "12") {
00170                         $iIntVal = 0;
00171                     }
00172 
00173                     $aTimeMatches[1] = sprintf("%02d", $iIntVal);
00174                 }
00175 
00176                 break;
00177             }
00178         }
00179 
00180         if (!$blTimeFound) {
00181             //return $sDate;
00182             // #871A. trying to keep date as possible correct
00183             $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
00184 
00185             return $oObject->value;
00186         }
00187 
00188         $this->_formatCorrectTimeValue($oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields);
00189 
00190         // on some cases we get empty value
00191         if (!$oObject->fldmax_length) {
00192             return $this->convertDBDateTime($oObject, $blToTimeStamp, $blOnlyDate);
00193         }
00194 
00195         return $oObject->value;
00196     }
00197 
00207     public function convertDBTimestamp($oObject, $blToTimeStamp = false)
00208     {
00209         // on this case usually means that we gonna save value, and value is formatted, not plain
00210         $sSQLTimeStampPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/";
00211         $sISOTimeStampPattern = "/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/";
00212         $aMatches = array();
00213         $oStr = getStr();
00214 
00215         // preparing value to save
00216         if ($blToTimeStamp) {
00217             // reformatting value to ISO
00218             $this->convertDBDateTime($oObject, $blToTimeStamp);
00219 
00220             if ($oStr->preg_match($sISOTimeStampPattern, $oObject->value, $aMatches)) {
00221                 // changing layout
00222                 $oObject->setValue($aMatches[1] . $aMatches[2] . $aMatches[3] . $aMatches[4] . $aMatches[5] . $aMatches[6]);
00223                 $oObject->fldmax_length = strlen($oObject->value);
00224 
00225                 return $oObject->value;
00226             }
00227         } else {
00228             // loading and formatting value
00229             // checking and parsing SQL timestamp value
00230             //$sSQLTimeStampPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/";
00231             if ($oStr->preg_match($sSQLTimeStampPattern, $oObject->value, $aMatches)) {
00232                 $iTimestamp = mktime(
00233                     $aMatches[4], //h
00234                     $aMatches[5], //m
00235                     $aMatches[6], //s
00236                     $aMatches[2], //M
00237                     $aMatches[3], //d
00238                     $aMatches[1]
00239                 ); //y
00240                 if (!$iTimestamp) {
00241                     $iTimestamp = "0";
00242                 }
00243 
00244                 $oObject->setValue(trim(date("Y-m-d H:i:s", $iTimestamp)));
00245                 $oObject->fldmax_length = strlen($oObject->value);
00246                 $this->convertDBDateTime($oObject, $blToTimeStamp);
00247 
00248                 return $oObject->value;
00249             }
00250         }
00251     }
00252 
00261     public function convertDBDate($oObject, $blToTimeStamp = false)
00262     {
00263         return $this->convertDBDateTime($oObject, $blToTimeStamp, true);
00264     }
00265 
00277     protected function _setDefaultFormatedValue($oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate)
00278     {
00279         $aDefTimePatterns = $this->_defaultTimePattern();
00280         $aDFormats = $this->_defineDateFormattingRules();
00281         $aTFormats = $this->_defineTimeFormattingRules();
00282         $oStr = getStr();
00283 
00284         foreach (array_keys($aDefTimePatterns) as $sDefTimePattern) {
00285             if ($oStr->preg_match($sDefTimePattern, $sDate)) {
00286                 $blDefTimeFound = true;
00287                 break;
00288             }
00289         }
00290 
00291         // setting and returning default formatted value
00292         if ($blOnlyDate) {
00293             $oObject->setValue(trim($aDFormats[$sLocalDateFormat][2])); // . " " . @$aTFormats[$sLocalTimeFormat][2]);
00294             // increasing(decreasing) field length
00295             $oObject->fldmax_length = strlen($oObject->value);
00296 
00297             return;
00298         } elseif ($blDefTimeFound) {
00299             // setting value
00300             $oObject->setValue(trim($aDFormats[$sLocalDateFormat][2] . " " . $aTFormats[$sLocalTimeFormat][2]));
00301             // increasing(decreasing) field length
00302             $oObject->fldmax_length = strlen($oObject->value);
00303 
00304             return;
00305         }
00306     }
00307 
00315     protected function _defineAndCheckDefaultTimeValues($blToTimeStamp)
00316     {
00317         // defining time format
00318         // checking for default values
00319         $sLocalTimeFormat = oxRegistry::getConfig()->getConfigParam('sLocalTimeFormat');
00320         if (!$sLocalTimeFormat || $blToTimeStamp) {
00321             $sLocalTimeFormat = "ISO";
00322         }
00323 
00324         return $sLocalTimeFormat;
00325     }
00326 
00334     protected function _defineAndCheckDefaultDateValues($blToTimeStamp)
00335     {
00336         // defining time format
00337         // checking for default values
00338         $sLocalDateFormat = oxRegistry::getConfig()->getConfigParam('sLocalDateFormat');
00339         if (!$sLocalDateFormat || $blToTimeStamp) {
00340             $sLocalDateFormat = "ISO";
00341         }
00342 
00343         return $sLocalDateFormat;
00344     }
00345 
00351     protected function _defaultDatePattern()
00352     {
00353         // default date patterns
00354         $aDefDatePatterns = array("/^0000-00-00/"   => "ISO",
00355                                   "/^00\.00\.0000/" => "EUR",
00356                                   "/^00\/00\/0000/" => "USA"
00357         );
00358 
00359         return $aDefDatePatterns;
00360     }
00361 
00367     protected function _defaultTimePattern()
00368     {
00369         // default time patterns
00370         $aDefTimePatterns = array("/00:00:00$/"    => "ISO",
00371                                   "/00\.00\.00$/"  => "EUR",
00372                                   "/00:00:00 AM$/" => "USA"
00373         );
00374 
00375         return $aDefTimePatterns;
00376     }
00377 
00383     protected function _regexp2ValidateDateInput()
00384     {
00385         // regexps to validate input
00386         $aDatePatterns = array("/^([0-9]{4})-([0-9]{2})-([0-9]{2})/"   => "ISO",
00387                                "/^([0-9]{2})\.([0-9]{2})\.([0-9]{4})/" => "EUR",
00388                                "/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})/" => "USA"
00389         );
00390 
00391         return $aDatePatterns;
00392     }
00393 
00399     protected function _regexp2ValidateTimeInput()
00400     {
00401         // regexps to validate input
00402         $aTimePatterns = array("/([0-9]{2}):([0-9]{2}):([0-9]{2})$/"                 => "ISO",
00403                                "/([0-9]{2})\.([0-9]{2})\.([0-9]{2})$/"               => "EUR",
00404                                "/([0-9]{2}):([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/" => "USA"
00405         );
00406 
00407         return $aTimePatterns;
00408     }
00409 
00415     protected function _defineDateFormattingRules()
00416     {
00417         // date formatting rules
00418         $aDFormats = array("ISO" => array("Y-m-d", array(2, 3, 1), "0000-00-00"),
00419                            "EUR" => array("d.m.Y", array(2, 1, 3), "00.00.0000"),
00420                            "USA" => array("m/d/Y", array(1, 2, 3), "00/00/0000")
00421         );
00422 
00423         return $aDFormats;
00424     }
00425 
00431     protected function _defineTimeFormattingRules()
00432     {
00433         // time formatting rules
00434         $aTFormats = array("ISO" => array("H:i:s", array(1, 2, 3), "00:00:00"),
00435                            "EUR" => array("H.i.s", array(1, 2, 3), "00.00.00"),
00436                            "USA" => array("h:i:s A", array(1, 2, 3), "00:00:00 AM")
00437         );
00438 
00439         return $aTFormats;
00440     }
00441 
00450     protected function _setDefaultDateTimeValue($oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate)
00451     {
00452         $aDFormats = $this->_defineDateFormattingRules();
00453         $aTFormats = $this->_defineTimeFormattingRules();
00454 
00455         $sReturn = $aDFormats[$sLocalDateFormat][2];
00456         if (!$blOnlyDate) {
00457             $sReturn .= " " . $aTFormats[$sLocalTimeFormat][2];
00458         }
00459 
00460         if ($oObject instanceof oxField) {
00461             $oObject->setValue(trim($sReturn));
00462         } else {
00463             $oObject->value = trim($sReturn);
00464         }
00465         // increasing(decreasing) field lenght
00466         $oObject->fldmax_length = strlen($oObject->value);
00467     }
00468 
00477     protected function _setDate($oObject, $sDateFormat, $aDFields, $aDateMatches)
00478     {
00479         // formatting correct time value
00480         $iTimestamp = mktime(
00481             0, 0, 0, $aDateMatches[$aDFields[0]],
00482             $aDateMatches[$aDFields[1]],
00483             $aDateMatches[$aDFields[2]]
00484         );
00485 
00486         if ($oObject instanceof oxField) {
00487             $oObject->setValue(@date($sDateFormat, $iTimestamp));
00488         } else {
00489             $oObject->value = @date($sDateFormat, $iTimestamp);
00490         }
00491         // we should increase (decrease) field lenght
00492         $oObject->fldmax_length = strlen($oObject->value);
00493     }
00494 
00506     protected function _formatCorrectTimeValue($oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields)
00507     {
00508         // formatting correct time value
00509         $iTimestamp = @mktime(
00510             (int) $aTimeMatches[$aTFields[0]],
00511             (int) $aTimeMatches[$aTFields[1]],
00512             (int) $aTimeMatches[$aTFields[2]],
00513             (int) $aDateMatches[$aDFields[0]],
00514             (int) $aDateMatches[$aDFields[1]],
00515             (int) $aDateMatches[$aDFields[2]]
00516         );
00517 
00518         if ($oObject instanceof oxField) {
00519             $oObject->setValue(trim(@date($sDateFormat . " " . $sTimeFormat, $iTimestamp)));
00520         } else {
00521             $oObject->value = trim(@date($sDateFormat . " " . $sTimeFormat, $iTimestamp));
00522         }
00523 
00524         // we should increase (decrease) field lenght
00525         $oObject->fldmax_length = strlen($oObject->value);
00526     }
00527 
00534     public function getTime()
00535     {
00536         return $this->shiftServerTime(time());
00537     }
00538 
00547     public function formTime($sTime = 'now', $sTime2 = null)
00548     {
00549         $oDate = new DateTime($sTime);
00550 
00551         if ($sTime2) {
00552             $aHourToCheck = explode(':', $sTime2);
00553             $iHour = $aHourToCheck[0];
00554             $iMinutes = $aHourToCheck[1];
00555             $iSecond = $aHourToCheck[2];
00556             $oDate->setTime($iHour, $iMinutes, $iSecond);
00557         }
00558 
00559         return $this->shiftServerTime($oDate->getTimestamp());
00560     }
00561 
00569     public function shiftServerTime($iTime)
00570     {
00571         $iServerTimeShift = $this->getConfig()->getConfigParam('iServerTimeShift');
00572         if ($iServerTimeShift) {
00573             $iTime = $iTime + ((int) $iServerTimeShift * 3600);
00574         }
00575         return $iTime;
00576     }
00577 
00589     public function getWeekNumber($iFirstWeekDay, $sTimestamp = null, $sFormat = null)
00590     {
00591         if ($sTimestamp == null) {
00592             $sTimestamp = time();
00593         }
00594         if ($sFormat == null) {
00595             $sFormat = '%W';
00596             if ($iFirstWeekDay) {
00597                 $sFormat = '%U';
00598             }
00599         }
00600 
00601         return (int) strftime($sFormat, $sTimestamp);
00602     }
00603 
00611     public function german2English($sDate)
00612     {
00613         $aDate = explode(".", $sDate);
00614 
00615         if (isset($aDate) && count($aDate) > 1) {
00616             if (count($aDate) == 2) {
00617                 $sDate = $aDate[1] . "-" . $aDate[0];
00618             } else {
00619                 $sDate = $aDate[2] . "-" . $aDate[1] . "-" . $aDate[0];
00620             }
00621         }
00622 
00623         return $sDate;
00624     }
00625 
00634     public function isEmptyDate($sDate)
00635     {
00636         $blIsEmpty = true;
00637 
00638         if (!empty($sDate)) {
00639             $sDate = preg_replace("/[^0-9a-z]/i", "", $sDate);
00640             if (is_numeric($sDate) && $sDate == 0) {
00641                 $blIsEmpty = true;
00642             } else {
00643                 $blIsEmpty = false;
00644             }
00645         }
00646 
00647         return $blIsEmpty;
00648     }
00649 
00660     protected function _processDate($aTime, $aDate, $blGerman, $sFormat)
00661     {
00662         if ($blGerman) {
00663             return date($sFormat, mktime($aTime[0], $aTime[1], $aTime[2], $aDate[1], $aDate[0], $aDate[2]));
00664         } else {
00665             return date($sFormat, mktime($aTime[0], $aTime[1], $aTime[2], $aDate[1], $aDate[2], $aDate[0]));
00666         }
00667     }
00668 }