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