1: <?php
2:
3: namespace Alpha\Model\Type;
4:
5: use Alpha\Util\Helper\Validator;
6: use Alpha\Exception\IllegalArguementException;
7: use Alpha\Util\Config\ConfigProvider;
8:
9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50:
51: class Timestamp extends Type implements TypeInterface
52: {
53: 54: 55: 56: 57: 58: 59:
60: private $year;
61:
62: 63: 64: 65: 66: 67: 68:
69: private $month;
70:
71: 72: 73: 74: 75: 76: 77:
78: private $day;
79:
80: 81: 82: 83: 84: 85: 86:
87: private $hour;
88:
89: 90: 91: 92: 93: 94: 95:
96: private $minute;
97:
98: 99: 100: 101: 102: 103: 104:
105: private $second;
106:
107: 108: 109: 110: 111: 112: 113:
114: private $weekday;
115:
116: 117: 118: 119: 120: 121: 122:
123: private $validationRule;
124:
125: 126: 127: 128: 129: 130: 131:
132: private $helper = 'Not a valid timestamp value! A timestamp should be in the format YYYY-MM-DD HH:MM:SS.';
133:
134: 135: 136: 137: 138: 139: 140:
141: public function __construct($timestamp = '')
142: {
143: $config = ConfigProvider::getInstance();
144:
145: $this->validationRule = Validator::ALLOW_ALL;
146:
147: if (empty($timestamp)) {
148: if ($config->get('app.default.datetime') == 'now') {
149: $this->year = date('Y');
150: $this->month = date('m');
151: $this->day = date('d');
152: $this->weekday = date('l');
153: $this->hour = date('H');
154: $this->minute = date('i');
155: $this->second = date('s');
156: } else {
157: $this->year = '0000';
158: $this->month = '00';
159: $this->day = '00';
160: $this->hour = '00';
161: $this->minute = '00';
162: $this->second = '00';
163: }
164: } else {
165: if (preg_match($this->validationRule, $timestamp)) {
166: $this->populateFromString($timestamp);
167: } else {
168: throw new IllegalArguementException($this->helper);
169: }
170: }
171: }
172:
173: 174: 175: 176: 177: 178: 179:
180: public function setValue($dateTime)
181: {
182: $this->populateFromString($dateTime);
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
199: public function setTimestampValue($year, $month, $day, $hour, $minute, $second)
200: {
201: $valid = null;
202:
203: if (!preg_match('/^[0-9]{4}$/', $year)) {
204: $valid = 'The year value '.$year.' provided is invalid!';
205: }
206: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) {
207: $valid = 'The month value '.$month.' provided is invalid!';
208: }
209: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) {
210: $valid = 'The day value '.$day.' provided is invalid!';
211: }
212: if (!isset($valid) && !checkdate($month, $day, $year)) {
213: $valid = 'The day value '.$year.'-'.$month.'-'.$day.' provided is invalid!';
214: }
215: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $hour) || !($hour >= 0 && $hour < 24)) {
216: $valid = 'The hour value '.$hour.' provided is invalid!';
217: }
218: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60)) {
219: $valid = 'The minute value '.$minute.' provided is invalid!';
220: }
221: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60)) {
222: $valid = 'The second value '.$second.' provided is invalid!';
223: }
224:
225: if (isset($valid)) {
226: throw new IllegalArguementException($valid);
227: } else {
228: $this->year = $year;
229: $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
230: $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
231: $this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
232: $this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
233: $this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
234: $unixTime = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
235: $this->weekday = date('l', $unixTime);
236: }
237: }
238:
239: 240: 241: 242: 243: 244: 245:
246: public function getValue()
247: {
248: return $this->year.'-'.$this->month.'-'.$this->day.' '.$this->hour.':'.$this->minute.':'.$this->second;
249: }
250:
251: 252: 253: 254: 255: 256: 257:
258: public function getUnixValue()
259: {
260: return mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: public function getDate()
271: {
272: return $this->year.'-'.$this->month.'-'.$this->day;
273: }
274:
275: 276: 277: 278: 279: 280: 281:
282: public function getEuroValue()
283: {
284: return $this->day.'/'.$this->month.'/'.mb_substr($this->year, 2, 2);
285: }
286:
287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297:
298: public function setDate($year, $month, $day)
299: {
300: $valid = null;
301:
302: if (!preg_match('/^[0-9]{4}$/', $year)) {
303: $valid = 'The year value '.$year.' provided is invalid!';
304: }
305: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) {
306: $valid = 'The month value '.$month.' provided is invalid!';
307: }
308: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) {
309: $valid = 'The day value '.$day.' provided is invalid!';
310: }
311: if (!isset($valid) && !checkdate($month, $day, $year)) {
312: $valid = 'The day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
313: }
314:
315: if (isset($valid)) {
316: throw new IllegalArguementException($valid);
317: } else {
318: $this->year = $year;
319: $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
320: $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
321: $unixTime = mktime(0, 0, 0, $this->month, $this->day, $this->year);
322: $this->weekday = date('l', $unixTime);
323: }
324: }
325:
326: 327: 328: 329: 330: 331: 332:
333: public function getTime()
334: {
335: return $this->hour.':'.$this->minute.':'.$this->second;
336: }
337:
338: 339: 340: 341: 342: 343: 344:
345: public function getYear()
346: {
347: return $this->year;
348: }
349:
350: 351: 352: 353: 354: 355: 356:
357: public function getMonth()
358: {
359: return $this->month;
360: }
361:
362: 363: 364: 365: 366: 367: 368:
369: public function getDay()
370: {
371: return $this->day;
372: }
373:
374: 375: 376: 377: 378: 379: 380:
381: public function getWeekday()
382: {
383: return $this->weekday;
384: }
385:
386: 387: 388: 389: 390: 391: 392:
393: public function getHour()
394: {
395: return $this->hour;
396: }
397:
398: 399: 400: 401: 402: 403: 404:
405: public function getMinute()
406: {
407: return $this->minute;
408: }
409:
410: 411: 412: 413: 414: 415: 416:
417: public function getSecond()
418: {
419: return $this->second;
420: }
421:
422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432:
433: public function setTime($hour, $minute, $second)
434: {
435: $valid = null;
436:
437: if (!isset($valid) && !preg_match('/^[0-9]{2}$/', $hour) || !($hour >= 0 && $hour < 24)) {
438: $valid = 'The hour value '.$hour.' provided is invalid!';
439: }
440: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60)) {
441: $valid = 'The minute value '.$minute.' provided is invalid!';
442: }
443: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60)) {
444: $valid = 'The second value '.$second.' provided is invalid!';
445: }
446:
447: if (isset($valid)) {
448: throw new IllegalArguementException($valid);
449: } else {
450: $this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
451: $this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
452: $this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
453: }
454: }
455:
456: 457: 458: 459: 460: 461: 462: 463: 464:
465: public function populateFromString($dateTime)
466: {
467: $valid = null;
468:
469: if ($dateTime == 'Please select' || $dateTime == '0000-00-00 00:00:00') {
470: $this->year = '0000';
471: $this->month = '00';
472: $this->day = '00';
473: $this->hour = '00';
474: $this->minute = '00';
475: $this->second = '00';
476: } else {
477: $spilt_by_space = explode(' ', $dateTime);
478:
479: if (isset($spilt_by_space[0])) {
480: $date = $spilt_by_space[0];
481: } else {
482: throw new IllegalArguementException($this->helper);
483: }
484:
485: if (isset($spilt_by_space[1])) {
486: $time = $spilt_by_space[1];
487: } else {
488: throw new IllegalArguementException($this->helper);
489: }
490:
491: $split_by_dash = explode('-', $date);
492:
493: if (isset($split_by_dash[0])) {
494: $year = $split_by_dash[0];
495: } else {
496: throw new IllegalArguementException($this->helper);
497: }
498:
499: if (isset($split_by_dash[1])) {
500: $month = $split_by_dash[1];
501: } else {
502: throw new IllegalArguementException($this->helper);
503: }
504:
505: if (isset($split_by_dash[2])) {
506: $day = $split_by_dash[2];
507: } else {
508: throw new IllegalArguementException($this->helper);
509: }
510:
511: $split_by_colon = explode(':', $time);
512:
513: if (isset($split_by_colon[0])) {
514: $hour = $split_by_colon[0];
515: } else {
516: throw new IllegalArguementException($this->helper);
517: }
518:
519: if (isset($split_by_colon[1])) {
520: $minute = $split_by_colon[1];
521: } else {
522: throw new IllegalArguementException($this->helper);
523: }
524:
525: if (isset($split_by_colon[2])) {
526: $second = $split_by_colon[2];
527: } else {
528: throw new IllegalArguementException($this->helper);
529: }
530:
531: if (!preg_match('/^[0-9]{4}$/', $year)) {
532: $valid = 'The year value '.$year.' provided is invalid!';
533: }
534: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $month)) {
535: $valid = 'The month value '.$month.' provided is invalid!';
536: }
537: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $day)) {
538: $valid = 'The day value '.$day.' provided is invalid!';
539: }
540: if (!isset($valid) && !checkdate($month, $day, $year)) {
541: $valid = 'The day value '.$year.'/'.$month.'/'.$day.' provided is invalid!';
542: }
543: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $hour) || !($hour >= 0 && $hour < 24)) {
544: $valid = 'The hour value '.$hour.' provided is invalid!';
545: }
546: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $minute) || !($minute >= 0 && $minute < 60)) {
547: $valid = 'The minute value '.$minute.' provided is invalid!';
548: }
549: if (!isset($valid) && !preg_match('/^[0-9]{1,2}$/', $second) || !($second >= 0 && $second < 60)) {
550: $valid = 'The second value '.$second.' provided is invalid!';
551: }
552:
553: if (isset($valid)) {
554: throw new IllegalArguementException($valid);
555: } else {
556: $this->year = $year;
557: $this->month = str_pad($month, 2, '0', STR_PAD_LEFT);
558: $this->day = str_pad($day, 2, '0', STR_PAD_LEFT);
559: $this->hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
560: $this->minute = str_pad($minute, 2, '0', STR_PAD_LEFT);
561: $this->second = str_pad($second, 2, '0', STR_PAD_LEFT);
562: $unixTime = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
563: $this->weekday = date('l', $unixTime);
564: }
565: }
566: }
567:
568: 569: 570: 571: 572: 573: 574:
575: public function getRule()
576: {
577: return $this->validationRule;
578: }
579:
580: 581: 582: 583: 584: 585: 586:
587: public function setRule($rule)
588: {
589: $this->validationRule = $rule;
590: }
591:
592: 593: 594: 595: 596: 597: 598:
599: public function getHelper()
600: {
601: return $this->helper;
602: }
603:
604: 605: 606: 607: 608: 609: 610:
611: public function setHelper($helper)
612: {
613: $this->helper = $helper;
614: }
615:
616: 617: 618: 619: 620: 621: 622:
623: public function getTimeAway()
624: {
625: $periods = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade');
626: $lengths = array('60', '60', '24', '7', '4.35', '12', '10');
627:
628: $now = time();
629: $unixTS = $this->getUnixValue();
630:
631: if ($now > $unixTS) {
632: $difference = $now - $unixTS;
633: $tense = 'ago';
634: } else {
635: $difference = $unixTS - $now;
636: $tense = 'from now';
637: }
638:
639: for ($i = 0; $difference >= $lengths[$i] && $i < count($lengths) - 1; ++$i) {
640:
641: $difference = round($difference / $lengths[$i]);
642: }
643:
644: $difference = round($difference);
645:
646: if ($difference != 1) {
647: $periods[$i] .= 's';
648: }
649:
650: return $difference.' '.$periods[$i].' '.$tense;
651: }
652: }
653: