note 82830 deleted from function.date-create by derick
| From: | derick@php.net | Date: | Tue, 13 May 2008 20:19:03 +0000 |
| Subject: | note 82830 deleted from function.date-create by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139361@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Here's a way to use the DateTime object to validate user submitted date inputs:
<?php
class DateTimeException extends Exception {
public function __construct ($message, $errornumber = NULL) {
parent::__construct($message, $errornumber);
}
public function __toString() {
return ("Error: $this->message");
}
}
class MyDateTime extends DateTime {
public function __construct($input) {
if (strtotime($input)) {
parent::__construct($input);
}
else {
$this->is_valid_date = FALSE;
throw new DateTimeException("Date (" . $input . ") is incorrect");
}
}
public function isValidDate() {
return $this->is_valid_date;
}
}
try {
$date = new DateTime("Not a date")
}
catch (DateTimeException $error) {
echo $error;
exit;
}
?>
Comments are very much appreciated