1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cDatatypeDateTime extends cDatatype {
25:
26: 27: 28: 29:
30: protected $_iFirstDayOfWeek;
31:
32: 33: 34: 35: 36: 37:
38: const FORMAT_UNIX = 1;
39:
40: 41: 42: 43: 44:
45: const FORMAT_ISO = 2;
46:
47: 48: 49: 50: 51:
52: const FORMAT_LOCALE = 3;
53:
54: 55: 56: 57: 58:
59: const FORMAT_LOCALE_TIMEONLY = 4;
60:
61: 62: 63: 64: 65:
66: const FORMAT_LOCALE_DATEONLY = 5;
67:
68: 69: 70: 71: 72:
73: const FORMAT_MYSQL = 6;
74:
75: 76: 77: 78: 79:
80: const FORMAT_CUSTOM = 99;
81:
82: 83: 84: 85: 86:
87: const SUNDAY = 0;
88:
89: 90: 91: 92: 93:
94: const MONDAY = 1;
95:
96: 97: 98: 99: 100:
101: const TUESDAY = 2;
102:
103: 104: 105: 106: 107:
108: const WEDNESDAY = 3;
109:
110: 111: 112: 113: 114:
115: const THURSDAY = 4;
116:
117: 118: 119: 120: 121:
122: const FRIDAY = 5;
123:
124: 125: 126: 127: 128:
129: const SATURDAY = 6;
130:
131: 132: 133:
134: public function __construct() {
135: $this->setTargetFormat(self::FORMAT_LOCALE);
136: $this->setSourceFormat(self::FORMAT_UNIX);
137:
138: $this->_iYear = 1970;
139: $this->_iMonth = 1;
140: $this->_iDay = 1;
141: $this->_iHour = 0;
142: $this->_iMinute = 0;
143: $this->_iSecond = 0;
144:
145: $this->setFirstDayOfWeek(self::MONDAY);
146: parent::__construct();
147: }
148:
149: 150: 151: 152:
153: public function setCustomTargetFormat($targetFormat) {
154: $this->_sCustomTargetFormat = $targetFormat;
155: }
156:
157: 158: 159: 160:
161: public function setCustomSourceFormat($sourceFormat) {
162: $this->_sCustomSourceFormat = $sourceFormat;
163: }
164:
165: 166: 167: 168:
169: public function setSourceFormat($iSourceFormat) {
170: $this->_cSourceFormat = $iSourceFormat;
171: }
172:
173: 174: 175: 176:
177: public function setTargetFormat($cTargetFormat) {
178: $this->_cTargetFormat = $cTargetFormat;
179: }
180:
181: 182: 183: 184:
185: public function setYear($iYear) {
186: $this->_iYear = $iYear;
187: }
188:
189: 190: 191: 192:
193: public function getYear() {
194: return $this->_iYear;
195: }
196:
197: 198: 199: 200:
201: public function setMonth($iMonth) {
202: $this->_iMonth = $iMonth;
203: }
204:
205: 206: 207: 208:
209: public function getMonth() {
210: return ($this->_iMonth);
211: }
212:
213: 214: 215: 216:
217: public function setDay($iDay) {
218: $this->_iDay = $iDay;
219: }
220:
221: 222: 223: 224:
225: public function getDay() {
226: return ($this->_iDay);
227: }
228:
229: 230: 231: 232: 233:
234: public function getMonthName($iMonth) {
235: switch ($iMonth) {
236: case 1:
237: return i18n("January");
238: case 2:
239: return i18n("February");
240: case 3:
241: return i18n("March");
242: case 4:
243: return i18n("April");
244: case 5:
245: return i18n("May");
246: case 6:
247: return i18n("June");
248: case 7:
249: return i18n("July");
250: case 8:
251: return i18n("August");
252: case 9:
253: return i18n("September");
254: case 10:
255: return i18n("October");
256: case 11:
257: return i18n("November");
258: case 12:
259: return i18n("December");
260: }
261: }
262:
263: 264: 265: 266: 267:
268: public function getDayName($iDayOfWeek) {
269: switch ($iDayOfWeek) {
270: case 0:
271: return i18n("Sunday");
272: case 1:
273: return i18n("Monday");
274: case 2:
275: return i18n("Tuesday");
276: case 3:
277: return i18n("Wednesday");
278: case 4:
279: return i18n("Thursday");
280: case 5:
281: return i18n("Friday");
282: case 6:
283: return i18n("Saturday");
284: case 7:
285: return i18n("Sunday");
286: default:
287: return false;
288: }
289: }
290:
291: 292: 293: 294:
295: public function getDayOrder() {
296: $aDays = array(
297: 0,
298: 1,
299: 2,
300: 3,
301: 4,
302: 5,
303: 6
304: );
305: $aRemainderDays = array_splice($aDays, 0, $this->_iFirstDayOfWeek);
306:
307: $aReturnDays = array_merge($aDays, $aRemainderDays);
308:
309: return $aReturnDays;
310: }
311:
312: 313: 314: 315: 316: 317:
318: public function getNumberOfMonthDays($iMonth = false, $iYear = false) {
319: if ($iMonth === false) {
320: $iMonth = $this->_iMonth;
321: }
322:
323: if ($iYear === false) {
324: $iYear = $this->_iYear;
325: }
326:
327: return date("t", mktime(0, 0, 0, $iMonth, 1, $iYear));
328: }
329:
330: 331: 332: 333:
334: public function setFirstDayOfWeek($iDay) {
335: $this->_iFirstDayOfWeek = $iDay;
336: }
337:
338: 339: 340: 341:
342: public function getFirstDayOfWeek() {
343: return $this->_iFirstDayOfWeek;
344: }
345:
346: 347: 348: 349:
350: public function getLeapDay() {
351: return end($this->getDayOrder());
352: }
353:
354: 355: 356: 357: 358: 359:
360: public function set($value, $iOverrideFormat = false) {
361: if ($value == "") {
362: return;
363: }
364:
365: if ($iOverrideFormat !== false) {
366: $iFormat = $iOverrideFormat;
367: } else {
368: $iFormat = $this->_cSourceFormat;
369: }
370:
371: switch ($iFormat) {
372: case self::FORMAT_UNIX:
373: $sTemporaryTimestamp = $value;
374: $this->_iYear = date("Y", $sTemporaryTimestamp);
375: $this->_iMonth = date("m", $sTemporaryTimestamp);
376: $this->_iDay = date("d", $sTemporaryTimestamp);
377: $this->_iHour = date("H", $sTemporaryTimestamp);
378: $this->_iMinute = date("i", $sTemporaryTimestamp);
379: $this->_iSecond = date("s", $sTemporaryTimestamp);
380:
381: break;
382: case self::FORMAT_ISO:
383: $sTemporaryTimestamp = strtotime($value);
384: $this->_iYear = date("Y", $sTemporaryTimestamp);
385: $this->_iMonth = date("m", $sTemporaryTimestamp);
386: $this->_iDay = date("d", $sTemporaryTimestamp);
387: $this->_iHour = date("H", $sTemporaryTimestamp);
388: $this->_iMinute = date("i", $sTemporaryTimestamp);
389: $this->_iSecond = date("s", $sTemporaryTimestamp);
390: break;
391: case self::FORMAT_MYSQL:
392: $sTimeformat = 'YmdHis';
393:
394: $targetFormat = str_replace('.', '\.', $sTimeformat);
395: $targetFormat = str_replace('d', '([0-9]{2,2})', $targetFormat);
396: $targetFormat = str_replace('m', '([0-9]{2,2})', $targetFormat);
397: $targetFormat = str_replace('Y', '([0-9]{4,4})', $targetFormat);
398: $targetFormat = str_replace('H', '([0-9]{2,2})', $targetFormat);
399: $targetFormat = str_replace('i', '([0-9]{2,2})', $targetFormat);
400: $targetFormat = str_replace('s', '([0-9]{2,2})', $targetFormat);
401:
402: preg_match_all('/([a-zA-Z])/', $sTimeformat, $placeholderRegs);
403:
404:
405: preg_match('/' . $targetFormat . '/', $value, $dateRegs);
406:
407: $finalDate = array();
408:
409:
410: foreach ($placeholderRegs[0] as $key => $placeholderReg) {
411: if (isset($dateRegs[$key])) {
412: $finalDate[$placeholderReg] = $dateRegs[$key + 1];
413: }
414: }
415:
416:
417: foreach ($finalDate as $placeHolder => $value) {
418: switch ($placeHolder) {
419: case "d":
420: $this->_iDay = $value;
421: break;
422: case "m":
423: $this->_iMonth = $value;
424: break;
425: case "Y":
426: $this->_iYear = $value;
427: break;
428: case "H":
429: $this->_iHour = $value;
430: break;
431: case "i":
432: $this->_iMinute = $value;
433: break;
434: case "s":
435: $this->_iSecond = $value;
436: break;
437: default:
438: break;
439: }
440: }
441:
442: break;
443: case self::FORMAT_CUSTOM:
444:
445:
446: $sourceFormat = str_replace('.', '\.', $this->_sCustomSourceFormat);
447: $sourceFormat = str_replace('%d', '([0-9]{2,2})', $sourceFormat);
448: $sourceFormat = str_replace('%m', '([0-9]{2,2})', $sourceFormat);
449: $sourceFormat = str_replace('%Y', '([0-9]{4,4})', $sourceFormat);
450:
451:
452: preg_match_all('/(%[a-zA-Z])/', $this->_sCustomSourceFormat, $placeholderRegs);
453:
454:
455: preg_match('/' . $sourceFormat . '/', $value, $dateRegs);
456:
457: $finalDate = array();
458:
459:
460: foreach ($placeholderRegs[0] as $key => $placeholderReg) {
461: if (isset($dateRegs[$key])) {
462: $finalDate[$placeholderReg] = $dateRegs[$key + 1];
463: }
464: }
465:
466:
467: foreach ($finalDate as $placeHolder => $value) {
468: switch ($placeHolder) {
469: case "%d":
470: $this->_iDay = $value;
471: break;
472: case "%m":
473: $this->_iMonth = $value;
474: break;
475: case "%Y":
476: $this->_iYear = $value;
477: break;
478: default:
479: break;
480: }
481: }
482: break;
483: default:
484: break;
485: }
486: }
487:
488: 489: 490: 491: 492: 493: 494: 495:
496: public function get($iOverrideFormat = false) {
497: if ($iOverrideFormat !== false) {
498: $iFormat = $iOverrideFormat;
499: } else {
500: $iFormat = $this->_cSourceFormat;
501: }
502:
503: switch ($iFormat) {
504: case self::FORMAT_ISO:
505: $sTemporaryTimestamp = mktime($this->_iHour, $this->_iMinute, $this->_iSecond, $this->_iMonth, $this->_iDay, $this->_iYear);
506:
507: return date("Y-m-d H:i:s", $sTemporaryTimestamp);
508: break;
509: case self::FORMAT_UNIX:
510: $sTemporaryTimestamp = mktime($this->_iHour, $this->_iMinute, $this->_iSecond, $this->_iMonth, $this->_iDay, $this->_iYear);
511:
512: return ($sTemporaryTimestamp);
513: break;
514: case self::FORMAT_CUSTOM:
515: return strftime($this->_sCustomSourceFormat, mktime($this->_iHour, $this->_iMinute, $this->_iSecond, $this->_iMonth, $this->_iDay, $this->_iYear));
516: break;
517: case self::FORMAT_MYSQL:
518: $sTemporaryTimestamp = mktime($this->_iHour, $this->_iMinute, $this->_iSecond, $this->_iMonth, $this->_iDay, $this->_iYear);
519:
520: return date("YmdHis", $sTemporaryTimestamp);
521: break;
522: default:
523: throw new cInvalidArgumentException('The given format is not yet supported.');
524: break;
525: }
526: }
527:
528: 529: 530: 531: 532:
533: public function render($iOverrideFormat = false) {
534: if ($iOverrideFormat !== false) {
535: $iFormat = $iOverrideFormat;
536: } else {
537: $iFormat = $this->_cTargetFormat;
538: }
539:
540: switch ($iFormat) {
541: case self::FORMAT_LOCALE_TIMEONLY:
542: $sTimeformat = getEffectiveSetting("dateformat", "time", "H:i:s");
543:
544: return date($sTimeformat, mktime($this->_iHour, $this->_iMinute, $this->iSecond, $this->_iMonth, $this->_iDay, $this->_iYear));
545: case self::FORMAT_LOCALE_DATEONLY:
546: $sTimeformat = getEffectiveSetting("dateformat", "date", "Y-m-d");
547:
548: return date($sTimeformat, mktime($this->_iHour, $this->_iMinute, $this->iSecond, $this->_iMonth, $this->_iDay, $this->_iYear));
549: case self::FORMAT_LOCALE:
550: $sTimeformat = getEffectiveSetting("dateformat", "full", "Y-m-d H:i:s");
551:
552: return date($sTimeformat, mktime($this->_iHour, $this->_iMinute, $this->iSecond, $this->_iMonth, $this->_iDay, $this->_iYear));
553: case self::FORMAT_CUSTOM:
554: return strftime($this->_sCustomTargetFormat, mktime($this->_iHour, $this->_iMinute, $this->_iSecond, $this->_iMonth, $this->_iDay, $this->_iYear));
555: break;
556: }
557: }
558:
559: 560: 561: 562: 563:
564: public function parse($value) {
565: if ($value == "") {
566: return;
567: }
568:
569: switch ($this->_cTargetFormat) {
570: case self::FORMAT_ISO:
571: $sTemporaryTimestamp = strtotime($value);
572: $this->_iYear = date("Y", $sTemporaryTimestamp);
573: $this->_iMonth = date("m", $sTemporaryTimestamp);
574: $this->_iDay = date("d", $sTemporaryTimestamp);
575: $this->_iHour = date("H", $sTemporaryTimestamp);
576: $this->_iMinute = date("i", $sTemporaryTimestamp);
577: $this->_iSecond = date("s", $sTemporaryTimestamp);
578: break;
579: case self::FORMAT_LOCALE_DATEONLY:
580: $sTimeformat = getEffectiveSetting('dateformat', 'date', 'Y-m-d');
581:
582: $targetFormat = str_replace('.', '\.', $sTimeformat);
583: $targetFormat = str_replace('d', '([0-9]{2,2})', $targetFormat);
584: $targetFormat = str_replace('m', '([0-9]{2,2})', $targetFormat);
585: $targetFormat = str_replace('Y', '([0-9]{4,4})', $targetFormat);
586:
587:
588: preg_match_all('/([a-zA-Z])/', $sTimeformat, $placeholderRegs);
589:
590:
591: preg_match('/' . $targetFormat . '/', $value, $dateRegs);
592:
593: $finalDate = array();
594:
595:
596: foreach ($placeholderRegs[0] as $key => $placeholderReg) {
597: if (isset($dateRegs[$key])) {
598: $finalDate[$placeholderReg] = $dateRegs[$key + 1];
599: }
600: }
601:
602:
603: foreach ($finalDate as $placeHolder => $value) {
604: switch ($placeHolder) {
605: case "d":
606: $this->_iDay = $value;
607: break;
608: case "m":
609: $this->_iMonth = $value;
610: break;
611: case "Y":
612: $this->_iYear = $value;
613: break;
614: default:
615: break;
616: }
617: }
618: break;
619: case self::FORMAT_LOCALE:
620: $sTimeformat = getEffectiveSetting('dateformat', 'full', 'Y-m-d H:i:s');
621:
622: $targetFormat = str_replace('.', '\.', $sTimeformat);
623: $targetFormat = str_replace('d', '([0-9]{2,2})', $targetFormat);
624: $targetFormat = str_replace('m', '([0-9]{2,2})', $targetFormat);
625: $targetFormat = str_replace('Y', '([0-9]{4,4})', $targetFormat);
626:
627:
628: preg_match_all('/(%[a-zA-Z])/', $this->_sCustomTargetFormat, $placeholderRegs);
629:
630:
631: preg_match('/' . $targetFormat . '/', $value, $dateRegs);
632:
633: $finalDate = array();
634:
635:
636: foreach ($placeholderRegs[0] as $key => $placeholderReg) {
637: if (isset($dateRegs[$key])) {
638: $finalDate[$placeholderReg] = $dateRegs[$key + 1];
639: }
640: }
641:
642:
643: foreach ($finalDate as $placeHolder => $value) {
644: switch ($placeHolder) {
645: case "%d":
646: $this->_iDay = $value;
647: break;
648: case "%m":
649: $this->_iMonth = $value;
650: break;
651: case "%Y":
652: $this->_iYear = $value;
653: break;
654: default:
655: break;
656: }
657: }
658: break;
659: case self::FORMAT_CUSTOM:
660:
661:
662: $targetFormat = str_replace('.', '\.', $this->_sCustomTargetFormat);
663: $targetFormat = str_replace('%d', '([0-9]{2,2})', $targetFormat);
664: $targetFormat = str_replace('%m', '([0-9]{2,2})', $targetFormat);
665: $targetFormat = str_replace('%Y', '([0-9]{4,4})', $targetFormat);
666:
667:
668: preg_match_all('/(%[a-zA-Z])/', $this->_sCustomTargetFormat, $placeholderRegs);
669:
670:
671: preg_match('/' . $targetFormat . '/', $value, $dateRegs);
672:
673: $finalDate = array();
674:
675:
676: foreach ($placeholderRegs[0] as $key => $placeholderReg) {
677: if (isset($dateRegs[$key])) {
678: $finalDate[$placeholderReg] = $dateRegs[$key + 1];
679: }
680: }
681:
682:
683: foreach ($finalDate as $placeHolder => $value) {
684: switch ($placeHolder) {
685: case "%d":
686: $this->_iDay = $value;
687: break;
688: case "%m":
689: $this->_iMonth = $value;
690: break;
691: case "%Y":
692: $this->_iYear = $value;
693: break;
694: default:
695: break;
696: }
697: }
698: break;
699: default:
700: break;
701: }
702: }
703: }
704: