@@ -187,6 +187,54 @@ public function getBoolean($key, $default = false)
187
187
return $ this ->filter ($ key , $ default , FILTER_VALIDATE_BOOLEAN );
188
188
}
189
189
190
+ /**
191
+ * Returns the parameter value converted to a DateTime object.
192
+ *
193
+ * @param string $key The parameter key
194
+ * @param string $format The expected date format
195
+ * @param \DateTime|null $default The default value if the parameter key does not exist
196
+ * @param \DateTimeZone|null $timeZone
197
+ *
198
+ * @return \DateTime|null
199
+ */
200
+ public function getDate ($ key , $ format = 'Y-m-d ' , $ default = null , $ timeZone = null )
201
+ {
202
+ if (!$ this ->has ($ key )) {
203
+ return $ default ;
204
+ }
205
+
206
+ $ time = $ this ->get ($ key );
207
+
8000
208
+ // if the user has specified a timezone then pass that
209
+ // otherwise do not even attempt to put a value but rather let the runtime decide
210
+ // the default value by itself
211
+ // this is in order to ensure compatibility with all php versions since
212
+ // some accept null as a TimeZone parameter and others do not
213
+ if ($ timeZone !== null ) {
214
+ $ result = \DateTime::createFromFormat ($ format , $ time , $ timeZone );
215
+ } else {
216
+ $ result = \DateTime::createFromFormat ($ format , $ time );
217
+ }
218
+
219
+ // Failure to parse the date according to the specified format will return null
220
+ return false === $ result ? null : $ result ;
221
+ }
222
+
223
+ /**
224
+ * Returns the parameter value converted to a DateTime object while also parsing the time.
225
+ *
226
+ * @param string $key The parameter key
227
+ * @param string $format The expected date format
228
+ * @param \DateTime|null $default The default value if the parameter key does not exist
229
+ * @param \DateTimeZone|null $timeZone
230
+ *
231
+ * @return \DateTime|null
232
+ */
233
+ public function getDateTime ($ key , $ format = 'Y-m-d H:i:s ' , $ default = null , \DateTimeZone $ timeZone = null )
234
+ {
235
+ return $ this ->getDate ($ key , $ format , $ default , $ timeZone );
236
+ }
237
+
190
238
/**
191
239
* Filter key.
192
240
*
0 commit comments