1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19: 20:
21: class Form_Element_Date extends Form_Element_Text
22: {
23: 24: 25: 26: 27: 28:
29: protected $format;
30:
31: 32: 33: 34: 35: 36: 37:
38: public function __construct($nom,$form)
39: {
40: parent::__construct($nom,$form);
41: $this->format = 'fr';
42: }
43:
44: 45: 46: 47: 48: 49: 50:
51: public function format($format)
52: {
53: $formatAutorise = array('fr','us','sql');
54: if(in_array($format,$formatAutorise))
55: $this->format = $format;
56: else
57: throw new InvalidArgumentException('Format : "'.$format.'" de date non reconnu');
58:
59: return $this;
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function isValid($value)
70: {
71: if(parent::isValid($value))
72: {
73: if(!$this->required && empty($value))
74: return true;
75: switch($this->format)
76: {
77: case 'fr' :
78: if(preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`',$value)>0 ||
79: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`',$value)>0 ||
80: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`',$value)>0)
81: {
82: if(strpos($value,':'))
83: {
84: $datetime = explode(' ',$value);
85: list($jour,$mois,$annee) = explode('/',$datetime[0]);
86: $minutes = explode(':',$datetime[1]);
87: $heure = $minutes[0];
88: $min =(isset($minutes[1]))?$minutes[1]:'00';
89: $sec =(isset($minutes[2]))?$minutes[2]:'00';
90: if(is_null($sec)){$sec='00';}
91: if(checkdate($mois,$jour,$annee) && $heure < 24 && $min <60 && $sec<60)
92: return true;
93: }
94: else
95: {
96: list($jour,$mois,$annee) = explode('/',$value);
97: if(checkdate($mois,$jour,$annee))
98: return true;
99: }
100: }
101: case 'us' :
102: if(preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`',$value)>0 ||
103: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`',$value)>0||
104: preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`',$value)>0)
105: {
106: if(strpos($value,':'))
107: {
108: $datetime = explode(' ',$value);
109: list($mois,$jour,$annee) = explode('/',$datetime[0]);
110: list($heure,$min,$sec) = explode(':',$datetime[1]);
111: if(is_null($sec)){$sec='00';}
112: if(checkdate($mois,$jour,$annee) && $heure < 24 && $min <60 && $sec<60)
113: return true;
114: }
115: else
116: {
117: list($mois,$jour,$annee) = explode('/',$value);
118: if(checkdate($mois,$jour,$annee))
119: return true;
120: }
121: }
122: case 'sql' :
123: if(preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2}$`',$value)>0 ||
124: preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`',$value)>0||
125: preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}$`',$value)>0)
126: {
127: if(strpos($value,':'))
128: {
129: $datetime = explode(' ',$value);
130: list($annee,$mois,$jour) = explode('/',$datetime[0]);
131: list($heure,$min,$sec) = explode(':',$datetime[1]);
132: if(is_null($sec)){$sec='00';}
133: if(checkdate($mois,$jour,$annee) && $heure < 24 && $min <60 && $sec<60)
134: return true;
135: }
136: else
137: {
138: list($annee,$mois,$jour) = explode('/',$value);
139: if(checkdate($mois,$jour,$annee))
140: return true;
141: }
142: }
143:
144: default :
145: $this->errorMsg = Form_Error::NOTDATE;
146: return false;
147: }
148: $this->errorMsg = Form_Error::NOTDATE;
149: return false;
150: }
151: }
152:
153: }
154: ?>