1

I am trying to validate Date format mm/dd/yyyy with the below code:

**Date is 1st Feb, 2014

$is_valid_date = date('m/d/Y', strtotime('02/01/2014')) == 02/01/2014;

I've tried checking format d-m-Y in above code, it was working fine:

$is_valid_date = date('d-m-Y', strtotime('01-02-2014')) == 01-02-2014;
if( ! $is_valid_date){
    echo 'invalid date';
}

How to do 1 scenario without using any regular expression ??

user3030089
  • 272
  • 1
  • 13

2 Answers2

1

You could explode by "/" and then validate the date with the function checkdate.

$date = '02/01/2014';
list($month, $day, $year) = explode("/", $date);
$is_valid_date = checkdate($month, $day, $year);

var_dump($is_valid_date);
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
1

make date into string by quotation

<?php
$is_valid_date = date('d/m/Y', strtotime('01-02-2014')) == "01/02/2014";

if(  $is_valid_date){
    echo 'Valid date';
} 
else if ( !$is_valid_date)
{
    echo 'invalid date';
}
?>
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20