Reputation: 111
Does anyone know of any good functions for validating a date via input text box in codeigniter like this: dd/mm/yyyy? Thanks
Upvotes: 1
Views: 3756
Reputation: 17977
you could also use a custom callback function
<?php
function valid_date($date)
{
$date_format = 'd-m-Y'; /* use dashes - dd/mm/yyyy */
$date = trim($date);
/* UK dates and strtotime() don't work with slashes,
so just do a quick replace */
$date = str_replace('/', '-', $date);
$time = strtotime($date);
$is_valid = date($date_format, $time) == $date;
if($is_valid)
{
return true;
}
/* not a valid date..return false */
return false;
}
?>
your validation rule looks like:
$rules['your_date_field'] = "callback_valid_date";
Upvotes: 2
Reputation: 6346
Use strtotime function.
$date = strtotime($input);
returns false if invalid, or -1 for php4.
Upvotes: 2
Reputation: 3115
You're probably looking for the strptime
function. For the specified format you can use it like this:
$result = strptime($input, '%d/%m/%Y');
$result
will be either false
if the input is invalid or on array with the date.
Or if you're on Windows or have PHP >= 5.3, consider using date_parse_from_format
.
Upvotes: 2
Reputation: 10490
dont know about codeigniter but there's some alike already in php already checkdate();
Upvotes: 0