Skip to content
Advertisement

PHP checkdate variants

I found a php function checkdate() , but strangely enough it only seems to accept data in format of int $month , int $day , int $year. However I am passing the date as a string (example "2012-06-13") so I came up with this workaround, because I would only allow date entered in such format. Unfortunately I am feeling this is both insecure and not a nice approach to the problem:

function CheckAdditional($value)
{
    $data = explode("-", $value);

    return checkdate($data[1], $data[2], $data[0]);
}

Question: is there a better way to check whether the date is valid?

Advertisement

Answer

<?php

function CheckAdditional($value)
{
    return date('Y-m-d', strtotime($value)) == $value;
}

?>

After several tests from both me and the people who tried to help me with my answer, I came up with this which suits me perfectly fine and is both easy and really reliable solution in my opinion as none was able to prove it wrong so far.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement