Hi,
Does anyone know of any good functions for validating a date via input text box in codeigniter like this: dd/mm/yyyy? Thanks
Hi,
Does anyone know of any good functions for validating a date via input text box in codeigniter like this: dd/mm/yyyy? Thanks
dont know about codeigniter but there's some alike already in php already checkdate();
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
.
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";