Hi guys! Continuing our series of posts on the DataValidator class , today we are going to address the is_date () method , which, as the name says, is a function for validating fields of type Data (or Date if you prefer) .
The method is very flexible because it allows the validation of both instances of the DateTime class and a date (including time) in a specific format defined by the user.See a validation using an instance of the DateTime class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ini_set('default_charset','UTF-8'); include 'DataValidator.php'; $validate = new Data_Validator(); $date = new DateTime; $validate->set('date', $date)->is_date(); if($validate->validate()){ echo 'Thats ok!'; } else{ print_r($validate->get_errors()); } |
Now see that if you don’t want a DateTime object, you can pass a certain date and inform via parameter which format it should obey.
1 2 3 4 5 6 7 8 9 | $date = '12/31/2013'; //Date in format m/d/Y $validate->set('date', $date)->is_date('m/d/Y'); if($validate->validate()){ echo 'Thats ok!'; } else{ print_r($validate->get_errors()); } |
Now let’s look at a time validation in the format HH: mm: ss (Tip: Feel free to pass returns from the date () function as parameters).
1 2 3 4 5 6 7 8 9 | $date = date('H:i:s'); //Get the current time in the format HH:mm:ss $validate->set('date', $date)->is_date('H:i:s'); if($validate->validate()){ echo 'Thats ok!'; } else{ print_r($validate->get_errors()); } |
Well, I believe that’s it.
Hugs!!!