Viewed   117 times

I'm trying to validate date using PHP.

I'd like following formats to be valid:

d/m/yy
d/m/yyyy
dd/m/yy
dd/m/yyyy
d/mm/yy
d/mm/yyyy
dd/mm/yy
dd/mm/yyyy

I've tried many regular expressions and different variations of checkdate() function. Currently I have something like this:

function _date_is_valid($str)
{
     if(strlen($str) == 0)
         return TRUE;
     if(substr_count($str,'/') == 2)
     {
         if (preg_match("/^((((31/(0?[13578]|1[02]))|((29|30)/(0?[1,3-9]|1[0-2])))/(1[6-9]|[2-9]d)?d{2})|(29/0?2/(((1[6-9]|[2-9]d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1d|2[0-8])/((0?[1-9])|(1[0-2]))/((1[6-9]|[2-9]d)?d{2}))/", $str))
         {
             $datearray = explode('/',$str);
             if($datearray[2] > 2030)
                 return FALSE;
             return checkdate($datearray[1], $datearray[0], $datearray[2]);
         } 
         else 
         {
             return FALSE;
         }
     }
     return FALSE;
}

This however, validates dates like 11/11/200 and 11/11/200#

How can I validate date to match required format?

Edit: I could check datearray[2] to be between 10 and 30 and 2010 and 2030. But is there a way to check it using regex?

Edit1: return TRUE on strlen($str) == 0 is because I want users to be able to add events without knowing when will the event occur so someone else can qualify the schedule and assign event to certain date later


Just for the record. I ended up doing:

function _date_is_valid($str)
{
    if(strlen($str) == 0) //To accept entries without a date
        return TRUE;
    if(substr_count($str,'/') == 2)
    {
        list($d,$m,$y) = explode('/',$str);
        if(($y >= 10 && $y <= 30) || ($y >= 2010 && $y <= 2030))
        {
            return checkdate($m,$d,$y);
        }
    }
    return FALSE;
}

Thanks for all your answers

 Answers

4
function _date_is_valid($str) {
    if (substr_count($str, '/') == 2) {
        list($d, $m, $y) = explode('/', $str);
        return checkdate($m, $d, sprintf('%04u', $y));
    }

    return false;
}
Monday, September 26, 2022
5

A fast but sometimes-unreliable solution:

$date = '2011-09-02T18:00:00';

$time = strtotime($date);

$fixed = date('l, F jS Y at g:ia', $time); // 'a' and 't' are escaped as they are a format char.

Format characters detailed here.

Monday, August 1, 2022
2

You could use setlocale function before calling date function:

$newLocale = setlocale(LC_TIME, 'de_DE', 'de_DE.UTF-8');

EDIT: Quote from strftime docs:

This example will work if you have the respective locales installed in your system.

Saturday, October 22, 2022
 
5

Using regular expression to validate emails is tricky

Try the following email as an input to your regex ie:^[w.-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}$

abc@b...com

You can read more about email regex validation at http://www.regular-expressions.info/email.html

If you are doing this for an app then use email validation by sending an email to the address provided rather than using very complex regex.

Thursday, October 20, 2022
5

You're separating the date with dashes and the regex is looking for slashes?

Try

if ( !preg_match( "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $_POST['variant']['sales_start'] ) )
{ 
    echo "invalid";
}
Friday, August 5, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :