2

I have this php function for validation date format:

 function _CHECK_DATEFORMAT_($date, $format='YYYY/MM/DD')
   {
    switch( $format )
    {
        case 'YYYY/MM/DD':
        case 'YYYY-MM-DD':
        list( $y, $m, $d ) = preg_split( '/[-\.\/ ]/', $date );
        break;

        case 'YYYY/DD/MM':
        case 'YYYY-DD-MM':
        list( $y, $d, $m ) = preg_split( '/[-\.\/ ]/', $date );
        break;

        case 'DD-MM-YYYY':
        case 'DD/MM/YYYY':
        list( $d, $m, $y ) = preg_split( '/[-\.\/ ]/', $date ); //3715 LINE
        break;

        case 'MM-DD-YYYY':
        case 'MM/DD/YYYY':
        list( $m, $d, $y ) = preg_split( '/[-\.\/ ]/', $date );
        break;

        case 'YYYYMMDD':
        $y = substr( $date, 0, 4 );
        $m = substr( $date, 4, 2 );
        $d = substr( $date, 6, 2 );
        break;

        case 'YYYYDDMM':
        $y = substr( $date, 0, 4 );
        $d = substr( $date, 4, 2 );
        $m = substr( $date, 6, 2 );
        break;

        default:
        throw new Exception( "Invalid Date Format" );
    }
    return checkdate( $m, $d, $y ); //3738 Line
}

Code Output :

echo validateDate( '2007-21-04', 'YYYY-DD-MM' )  ? 'good'. "\n" : 'bad' . "\n";

This Worked but if user input invalid date character Like This :

echo validateDate( '2007*2104', 'YYYY-DD-MM' )  ? 'good'. "\n" : 'bad' . "\n";
echo validateDate( 'test', 'YYYY-DD-MM' )  ? 'good'. "\n" : 'bad' . "\n";

I see error:

Notice: Undefined offset: 2 in C:\xampp\htdocs\cms\functions.php on line 3715

Notice: Undefined offset: 1 in C:\xampp\htdocs\cms\functions.php on line 3715

Warning: checkdate() expects parameter 2 to be long, string given in C:\xampp\htdocs\cms\functions.php on line 3738

how do validate character before check for validate date format?

user27133
  • 487
  • 4
  • 16
  • 31
  • Where is your validateDate function? – AndiPower Jul 24 '14 at 08:46
  • `function _CHECK_DATEFORMAT_($date, $format) { return !!DateTime::createFromFormat($format, $date); }` - Parsing dates is easier if you use the default functions, like createFromFormat, which parses the format and returns the datetime to you, or returns false. Either way `!!` converts it to boolean. – scragar Jul 24 '14 at 08:46
  • You should really limit the formats more, and only accept one way of writing dates. It will make everything a lot easier. – OptimusCrime Jul 24 '14 at 08:52

3 Answers3

1

Try This :

E better class for validating and formatting dates Without DateTime function :

http://www.tonymarston.net/php-mysql/dateclass.html

Or

You could use PHP's DateTime class:

function validateDate($date)
{
    $d = DateTime::createFromFormat('Y/m/d', $date);
    return $d && $d->format('Y/m/d') == $date;
}

function was copied from this answer or php.net

Community
  • 1
  • 1
ಠ_ಠ
  • 1,235
  • 2
  • 17
  • 29
  • Not to be rude or anything, but that class was written before PHP had the more native DateTime class, why should we choose that class over the built in one which offers better support and faster access? – scragar Jul 24 '14 at 08:53
  • @scragar: I Share this for this Q(without PHP Native DateTime Class) . you right DateTime is better. – ಠ_ಠ Jul 24 '14 at 09:01
0

date passed may obviously contain 1 or 2 slashes/minuses in, making list assignment inappropriate.

Warning is because checkdate expects integer params.

return checkdate( intval($m), intval($d), intval($y) ); //3738 Line
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

You can Use native php class DateTime for convert date string with special format to date

function _CHECK_DATEFORMAT_($date, $format='YYYY/MM/DD')
   {
    switch( $format )
    {
        case 'YYYY/MM/DD':
            $n_format="Y/m/d";
            break;
        case 'YYYY-MM-DD':
            $n_format="Y-m-d";
            break;
        case 'YYYY/DD/MM':
            $n_format="Y/d/m";
            break;
        case 'YYYY-DD-MM':
            $n_format="Y-d-m";
            break;
        case 'DD-MM-YYYY':
            $n_format="d-m-Y";
            break;
        case 'DD/MM/YYYY':
            $n_format="d/m/Y";
            break;
        case 'MM-DD-YYYY':
            $n_format="m-d-Y";
            break;
        case 'MM/DD/YYYY':
            $n_format="m/d/Y";
            break;
        case 'YYYYMMDD':
            $n_format="Ymd";
            break;
        case 'YYYYDDMM':
            $n_format="Ydm";
            break;
        default:
        throw new Exception( "Invalid Date Format" );
    }
    $date=DateTime::createFromFormat($n_format, $date);
    if(!$date){
        throw new Exception('Invalid Date by format '.$format);
    }
    $m=$date->format('m');
    $d=$date->format('d');
    $y=$date->format('Y');
    return checkdate( $m, $d, $y ); //3738 Line
}
Uvadzucumi
  • 11
  • 3