1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Validate\Validator;
14:
15: use Pry\Validate\ValidateAbstract;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: class Date extends ValidateAbstract
34: {
35:
36: const DATE_SQL = 'sql';
37: const DATE_SQLDATETIME = 'sqldatetime';
38: const DATE_FR = 'fr';
39: const DATE_US = 'us';
40:
41: private $format;
42:
43: public function __construct($format = 'fr')
44: {
45: $this->errorMsg = "n'est pas une date valide";
46: $this->format = $format;
47: }
48:
49: 50: 51: 52: 53: 54:
55: public function isValid($date)
56: {
57: switch ($this->format)
58: {
59: case self::DATE_FR:
60: {
61: if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $date))
62: {
63: list($d, $m, $y) = explode('/', $date);
64: if (checkdate($m, $d, $y))
65: return true;
66: }
67: return false;
68: }
69: case self::DATE_US:
70: {
71: if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $date))
72: {
73: list($m, $d, $y) = explode('/', $date);
74: if (checkdate($m, $d, $y))
75: return true;
76: }
77: return false;
78: }
79: case self::DATE_SQL:
80: {
81: if (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2}$`', $date))
82: {
83: list($y, $m, $d) = explode('-', $date);
84: if (checkdate($m, $d, $y))
85: return true;
86: }
87: return false;
88: }
89: case self::DATE_SQLDATETIME:
90: {
91: if (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`', $date))
92: {
93: list($date, $heure) = explode(' ', $date);
94: list($y, $m, $d) = explode('-', $date);
95: list($h, $mi, $s) = explode(':', $heure);
96: if (checkdate($m, $d, $y) && ($h >= 0 && $h < 24) && ($mi >= 0 && $mi < 60) && ($s >= 0 && $s < 60))
97: return true;
98: }
99: return false;
100: }
101: default:
102: break;
103: }
104: }
105:
106: }