PHP does not allow d / m / Y format in String date

I want to check a string that should be saved as a date. I want users to enter a date on the form format ('d-m-Y). I use this function to check if a valid date has been given:

function isDateValid($birthdate)
{
  $date = date('Y-m-d', $birthdate);        

  $stamp = strtotime(str_replace('/', '-', $date), $date);

  if (!is_numeric($stamp))
     return FALSE;

  //checkdate(month, day, year)
  if ( checkdate(date('n', $stamp), date('d', $stamp), date('Y', $stamp)) )
  {
     return TRUE;
  }
  return FALSE;
} 

This is great for every entry in this dmy format. But when d / m / Y is assigned to this format, it still returns true, but the date is saved as that m / d / Y in the database.

What can I do to prevent a record in this format 'd / m / y' from being saved to the database.

Thanks in advance

Solution found

Ok i think i found a solution

if(!preg_match('/^(\d\d?)-(\d\d?)-(\d\d\d\d)$/', $birthdate, $matches))
{
  return false;
}
return checkdate($matches[2], $matches[1], $matches[3]);

I check to see if the correct template has been set, after which I check if this is a valid date. This works great for me, thanks!

+3
source share
1

, : *\d{4}-\d{2}-\d{2}*,

0

All Articles