views:

59

answers:

4

Hi,

Does anyone know of any good functions for validating a date via input text box in codeigniter like this: dd/mm/yyyy? Thanks

A: 

dont know about codeigniter but there's some alike already in php already checkdate();

Breezer
+2  A: 

You're probably looking for the strptime function. For the specified format you can use it like this:

$result = strptime($input, '%d/%m/%Y');

$resultwill 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.

Matěj Grabovský
+2  A: 

Use strtotime function.

$date = strtotime($input);

returns false if invalid, or -1 for php4.

tpae
+1 for this being simple and quick, but note it will not work on OP's example as it is a UK date. try it and see
Ross
A: 

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";

Ross