OXID eShop CE  4.9.8
 All Classes Files Functions Variables Pages
oxutilsdate.php
Go to the documentation of this file.
1 <?php
2 
6 class oxUtilsDate extends oxSuperCfg
7 {
8 
17  public function formatDBDate($sDBDateIn, $blForceEnglishRet = false)
18  {
19  // convert english format to output format
20  if (!$sDBDateIn) {
21  return null;
22  }
23 
24  $oStr = getStr();
25  if ($blForceEnglishRet && $oStr->strstr($sDBDateIn, '-')) {
26  return $sDBDateIn;
27  }
28 
29  if ($this->isEmptyDate($sDBDateIn) && $sDBDateIn != '-') {
30  return '-';
31  } elseif ($sDBDateIn == '-') {
32  return '0000-00-00 00:00:00';
33  }
34 
35  // is it a timestamp ?
36  if (is_numeric($sDBDateIn)) {
37  // db timestamp : 20030322100409
38  $sNew = substr($sDBDateIn, 0, 4) . '-' . substr($sDBDateIn, 4, 2) . '-' . substr($sDBDateIn, 6, 2) . ' ';
39  // check if it is a timestamp or wrong data: 20030322
40  if (strlen($sDBDateIn) > 8) {
41  $sNew .= substr($sDBDateIn, 8, 2) . ':' . substr($sDBDateIn, 10, 2) . ':' . substr($sDBDateIn, 12, 2);
42  }
43  // convert it to english format
44  $sDBDateIn = $sNew;
45  }
46 
47  // remove time as it is same in english as in german
48  $aData = explode(' ', trim($sDBDateIn));
49 
50  // preparing time array
51  $sTime = (isset($aData[1]) && $oStr->strstr($aData[1], ':')) ? $aData[1] : '';
52  $aTime = $sTime ? explode(':', $sTime) : array(0, 0, 0);
53 
54  // preparing date array
55  $sDate = isset($aData[0]) ? $aData[0] : '';
56  $aDate = preg_split('/[\/.-]/', $sDate);
57 
58  // choosing format..
59  if ($sTime) {
60  $sFormat = $blForceEnglishRet ? 'Y-m-d H:i:s' : oxRegistry::getLang()->translateString('fullDateFormat');
61  } else {
62  $sFormat = $blForceEnglishRet ? 'Y-m-d' : oxRegistry::getLang()->translateString('simpleDateFormat');
63  }
64 
65  if (count($aDate) != 3) {
66  return date($sFormat);
67  } else {
68  return $this->_processDate($aTime, $aDate, $oStr->strstr($sDate, '.'), $sFormat);
69  }
70  }
71 
81  public function convertDBDateTime($oObject, $blToTimeStamp = false, $blOnlyDate = false)
82  {
83  $sDate = $oObject->value;
84 
85  // defining time format
86  $sLocalDateFormat = $this->_defineAndCheckDefaultDateValues($blToTimeStamp);
87  $sLocalTimeFormat = $this->_defineAndCheckDefaultTimeValues($blToTimeStamp);
88 
89  // default date/time patterns
90  $aDefDatePatterns = $this->_defaultDatePattern();
91 
92  // regexps to validate input
93  $aDatePatterns = $this->_regexp2ValidateDateInput();
94  $aTimePatterns = $this->_regexp2ValidateTimeInput();
95 
96  // date/time formatting rules
97  $aDFormats = $this->_defineDateFormattingRules();
98  $aTFormats = $this->_defineTimeFormattingRules();
99 
100  // empty date field value ? setting default value
101  if (!$sDate) {
102  $this->_setDefaultDateTimeValue($oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
103 
104  return $oObject->value;
105  }
106 
107  $blDefDateFound = false;
108  $oStr = getStr();
109 
110  // looking for default values that are formatted by MySQL
111  foreach (array_keys($aDefDatePatterns) as $sDefDatePattern) {
112  if ($oStr->preg_match($sDefDatePattern, $sDate)) {
113  $blDefDateFound = true;
114  break;
115  }
116  }
117 
118  // default value is set ?
119  if ($blDefDateFound) {
120  $this->_setDefaultFormatedValue($oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate);
121 
122  return $oObject->value;
123  }
124 
125  $blDateFound = false;
126  $blTimeFound = false;
127  $aDateMatches = array();
128  $aTimeMatches = array();
129 
130  // looking for date field
131  foreach ($aDatePatterns as $sPattern => $sType) {
132  if ($oStr->preg_match($sPattern, $sDate, $aDateMatches)) {
133  $blDateFound = true;
134 
135  // now we know the type of passed date
136  $sDateFormat = $aDFormats[$sLocalDateFormat][0];
137  $aDFields = $aDFormats[$sType][1];
138  break;
139  }
140  }
141 
142  // no such date field available ?
143  if (!$blDateFound) {
144  return $sDate;
145  }
146 
147  if ($blOnlyDate) {
148  $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
149 
150  return $oObject->value;
151  }
152 
153  // looking for time field
154  foreach ($aTimePatterns as $sPattern => $sType) {
155  if ($oStr->preg_match($sPattern, $sDate, $aTimeMatches)) {
156  $blTimeFound = true;
157 
158  // now we know the type of passed time
159  $sTimeFormat = $aTFormats[$sLocalTimeFormat][0];
160  $aTFields = $aTFormats[$sType][1];
161 
162  //
163  if ($sType == "USA" && isset($aTimeMatches[4])) {
164  $iIntVal = (int) $aTimeMatches[1];
165  if ($aTimeMatches[4] == "PM") {
166  if ($iIntVal < 13) {
167  $iIntVal += 12;
168  }
169  } elseif ($aTimeMatches[4] == "AM" && $aTimeMatches[1] == "12") {
170  $iIntVal = 0;
171  }
172 
173  $aTimeMatches[1] = sprintf("%02d", $iIntVal);
174  }
175 
176  break;
177  }
178  }
179 
180  if (!$blTimeFound) {
181  //return $sDate;
182  // #871A. trying to keep date as possible correct
183  $this->_setDate($oObject, $sDateFormat, $aDFields, $aDateMatches);
184 
185  return $oObject->value;
186  }
187 
188  $this->_formatCorrectTimeValue($oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields);
189 
190  // on some cases we get empty value
191  if (!$oObject->fldmax_length) {
192  return $this->convertDBDateTime($oObject, $blToTimeStamp, $blOnlyDate);
193  }
194 
195  return $oObject->value;
196  }
197 
207  public function convertDBTimestamp($oObject, $blToTimeStamp = false)
208  {
209  // on this case usually means that we gonna save value, and value is formatted, not plain
210  $sSQLTimeStampPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/";
211  $sISOTimeStampPattern = "/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/";
212  $aMatches = array();
213  $oStr = getStr();
214 
215  // preparing value to save
216  if ($blToTimeStamp) {
217  // reformatting value to ISO
218  $this->convertDBDateTime($oObject, $blToTimeStamp);
219 
220  if ($oStr->preg_match($sISOTimeStampPattern, $oObject->value, $aMatches)) {
221  // changing layout
222  $oObject->setValue($aMatches[1] . $aMatches[2] . $aMatches[3] . $aMatches[4] . $aMatches[5] . $aMatches[6]);
223  $oObject->fldmax_length = strlen($oObject->value);
224 
225  return $oObject->value;
226  }
227  } else {
228  // loading and formatting value
229  // checking and parsing SQL timestamp value
230  //$sSQLTimeStampPattern = "/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/";
231  if ($oStr->preg_match($sSQLTimeStampPattern, $oObject->value, $aMatches)) {
232  $iTimestamp = mktime(
233  $aMatches[4], //h
234  $aMatches[5], //m
235  $aMatches[6], //s
236  $aMatches[2], //M
237  $aMatches[3], //d
238  $aMatches[1]
239  ); //y
240  if (!$iTimestamp) {
241  $iTimestamp = "0";
242  }
243 
244  $oObject->setValue(trim(date("Y-m-d H:i:s", $iTimestamp)));
245  $oObject->fldmax_length = strlen($oObject->value);
246  $this->convertDBDateTime($oObject, $blToTimeStamp);
247 
248  return $oObject->value;
249  }
250  }
251  }
252 
261  public function convertDBDate($oObject, $blToTimeStamp = false)
262  {
263  return $this->convertDBDateTime($oObject, $blToTimeStamp, true);
264  }
265 
277  protected function _setDefaultFormatedValue($oObject, $sDate, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate)
278  {
279  $aDefTimePatterns = $this->_defaultTimePattern();
280  $aDFormats = $this->_defineDateFormattingRules();
281  $aTFormats = $this->_defineTimeFormattingRules();
282  $oStr = getStr();
283 
284  foreach (array_keys($aDefTimePatterns) as $sDefTimePattern) {
285  if ($oStr->preg_match($sDefTimePattern, $sDate)) {
286  $blDefTimeFound = true;
287  break;
288  }
289  }
290 
291  // setting and returning default formatted value
292  if ($blOnlyDate) {
293  $oObject->setValue(trim($aDFormats[$sLocalDateFormat][2])); // . " " . @$aTFormats[$sLocalTimeFormat][2]);
294  // increasing(decreasing) field length
295  $oObject->fldmax_length = strlen($oObject->value);
296 
297  return;
298  } elseif ($blDefTimeFound) {
299  // setting value
300  $oObject->setValue(trim($aDFormats[$sLocalDateFormat][2] . " " . $aTFormats[$sLocalTimeFormat][2]));
301  // increasing(decreasing) field length
302  $oObject->fldmax_length = strlen($oObject->value);
303 
304  return;
305  }
306  }
307 
315  protected function _defineAndCheckDefaultTimeValues($blToTimeStamp)
316  {
317  // defining time format
318  // checking for default values
319  $sLocalTimeFormat = oxRegistry::getConfig()->getConfigParam('sLocalTimeFormat');
320  if (!$sLocalTimeFormat || $blToTimeStamp) {
321  $sLocalTimeFormat = "ISO";
322  }
323 
324  return $sLocalTimeFormat;
325  }
326 
334  protected function _defineAndCheckDefaultDateValues($blToTimeStamp)
335  {
336  // defining time format
337  // checking for default values
338  $sLocalDateFormat = oxRegistry::getConfig()->getConfigParam('sLocalDateFormat');
339  if (!$sLocalDateFormat || $blToTimeStamp) {
340  $sLocalDateFormat = "ISO";
341  }
342 
343  return $sLocalDateFormat;
344  }
345 
351  protected function _defaultDatePattern()
352  {
353  // default date patterns
354  $aDefDatePatterns = array("/^0000-00-00/" => "ISO",
355  "/^00\.00\.0000/" => "EUR",
356  "/^00\/00\/0000/" => "USA"
357  );
358 
359  return $aDefDatePatterns;
360  }
361 
367  protected function _defaultTimePattern()
368  {
369  // default time patterns
370  $aDefTimePatterns = array("/00:00:00$/" => "ISO",
371  "/00\.00\.00$/" => "EUR",
372  "/00:00:00 AM$/" => "USA"
373  );
374 
375  return $aDefTimePatterns;
376  }
377 
383  protected function _regexp2ValidateDateInput()
384  {
385  // regexps to validate input
386  $aDatePatterns = array("/^([0-9]{4})-([0-9]{2})-([0-9]{2})/" => "ISO",
387  "/^([0-9]{2})\.([0-9]{2})\.([0-9]{4})/" => "EUR",
388  "/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})/" => "USA"
389  );
390 
391  return $aDatePatterns;
392  }
393 
399  protected function _regexp2ValidateTimeInput()
400  {
401  // regexps to validate input
402  $aTimePatterns = array("/([0-9]{2}):([0-9]{2}):([0-9]{2})$/" => "ISO",
403  "/([0-9]{2})\.([0-9]{2})\.([0-9]{2})$/" => "EUR",
404  "/([0-9]{2}):([0-9]{2}):([0-9]{2}) ([AP]{1}[M]{1})$/" => "USA"
405  );
406 
407  return $aTimePatterns;
408  }
409 
415  protected function _defineDateFormattingRules()
416  {
417  // date formatting rules
418  $aDFormats = array("ISO" => array("Y-m-d", array(2, 3, 1), "0000-00-00"),
419  "EUR" => array("d.m.Y", array(2, 1, 3), "00.00.0000"),
420  "USA" => array("m/d/Y", array(1, 2, 3), "00/00/0000")
421  );
422 
423  return $aDFormats;
424  }
425 
431  protected function _defineTimeFormattingRules()
432  {
433  // time formatting rules
434  $aTFormats = array("ISO" => array("H:i:s", array(1, 2, 3), "00:00:00"),
435  "EUR" => array("H.i.s", array(1, 2, 3), "00.00.00"),
436  "USA" => array("h:i:s A", array(1, 2, 3), "00:00:00 AM")
437  );
438 
439  return $aTFormats;
440  }
441 
450  protected function _setDefaultDateTimeValue($oObject, $sLocalDateFormat, $sLocalTimeFormat, $blOnlyDate)
451  {
452  $aDFormats = $this->_defineDateFormattingRules();
453  $aTFormats = $this->_defineTimeFormattingRules();
454 
455  $sReturn = $aDFormats[$sLocalDateFormat][2];
456  if (!$blOnlyDate) {
457  $sReturn .= " " . $aTFormats[$sLocalTimeFormat][2];
458  }
459 
460  if ($oObject instanceof oxField) {
461  $oObject->setValue(trim($sReturn));
462  } else {
463  $oObject->value = trim($sReturn);
464  }
465  // increasing(decreasing) field lenght
466  $oObject->fldmax_length = strlen($oObject->value);
467  }
468 
477  protected function _setDate($oObject, $sDateFormat, $aDFields, $aDateMatches)
478  {
479  // formatting correct time value
480  $iTimestamp = mktime(
481  0, 0, 0, $aDateMatches[$aDFields[0]],
482  $aDateMatches[$aDFields[1]],
483  $aDateMatches[$aDFields[2]]
484  );
485 
486  if ($oObject instanceof oxField) {
487  $oObject->setValue(@date($sDateFormat, $iTimestamp));
488  } else {
489  $oObject->value = @date($sDateFormat, $iTimestamp);
490  }
491  // we should increase (decrease) field lenght
492  $oObject->fldmax_length = strlen($oObject->value);
493  }
494 
506  protected function _formatCorrectTimeValue($oObject, $sDateFormat, $sTimeFormat, $aDateMatches, $aTimeMatches, $aTFields, $aDFields)
507  {
508  // formatting correct time value
509  $iTimestamp = @mktime(
510  (int) $aTimeMatches[$aTFields[0]],
511  (int) $aTimeMatches[$aTFields[1]],
512  (int) $aTimeMatches[$aTFields[2]],
513  (int) $aDateMatches[$aDFields[0]],
514  (int) $aDateMatches[$aDFields[1]],
515  (int) $aDateMatches[$aDFields[2]]
516  );
517 
518  if ($oObject instanceof oxField) {
519  $oObject->setValue(trim(@date($sDateFormat . " " . $sTimeFormat, $iTimestamp)));
520  } else {
521  $oObject->value = trim(@date($sDateFormat . " " . $sTimeFormat, $iTimestamp));
522  }
523 
524  // we should increase (decrease) field lenght
525  $oObject->fldmax_length = strlen($oObject->value);
526  }
527 
534  public function getTime()
535  {
536  return $this->shiftServerTime(time());
537  }
538 
547  public function formTime($sTime = 'now', $sTime2 = null)
548  {
549  $oDate = new DateTime($sTime);
550 
551  if ($sTime2) {
552  $aHourToCheck = explode(':', $sTime2);
553  $iHour = $aHourToCheck[0];
554  $iMinutes = $aHourToCheck[1];
555  $iSecond = $aHourToCheck[2];
556  $oDate->setTime($iHour, $iMinutes, $iSecond);
557  }
558 
559  return $this->shiftServerTime($oDate->getTimestamp());
560  }
561 
569  public function shiftServerTime($iTime)
570  {
571  $iServerTimeShift = $this->getConfig()->getConfigParam('iServerTimeShift');
572  if ($iServerTimeShift) {
573  $iTime = $iTime + ((int) $iServerTimeShift * 3600);
574  }
575  return $iTime;
576  }
577 
589  public function getWeekNumber($iFirstWeekDay, $sTimestamp = null, $sFormat = null)
590  {
591  if ($sTimestamp == null) {
592  $sTimestamp = time();
593  }
594  if ($sFormat == null) {
595  $sFormat = '%W';
596  if ($iFirstWeekDay) {
597  $sFormat = '%U';
598  }
599  }
600 
601  return (int) strftime($sFormat, $sTimestamp);
602  }
603 
611  public function german2English($sDate)
612  {
613  $aDate = explode(".", $sDate);
614 
615  if (isset($aDate) && count($aDate) > 1) {
616  if (count($aDate) == 2) {
617  $sDate = $aDate[1] . "-" . $aDate[0];
618  } else {
619  $sDate = $aDate[2] . "-" . $aDate[1] . "-" . $aDate[0];
620  }
621  }
622 
623  return $sDate;
624  }
625 
634  public function isEmptyDate($sDate)
635  {
636  $blIsEmpty = true;
637 
638  if (!empty($sDate)) {
639  $sDate = preg_replace("/[^0-9a-z]/i", "", $sDate);
640  if (is_numeric($sDate) && $sDate == 0) {
641  $blIsEmpty = true;
642  } else {
643  $blIsEmpty = false;
644  }
645  }
646 
647  return $blIsEmpty;
648  }
649 
660  protected function _processDate($aTime, $aDate, $blGerman, $sFormat)
661  {
662  if ($blGerman) {
663  return date($sFormat, mktime($aTime[0], $aTime[1], $aTime[2], $aDate[1], $aDate[0], $aDate[2]));
664  } else {
665  return date($sFormat, mktime($aTime[0], $aTime[1], $aTime[2], $aDate[1], $aDate[2], $aDate[0]));
666  }
667  }
668 }