PHP Regex to check date is in YYYY-MM-DD format

前端 未结 23 1412
终归单人心
终归单人心 2020-11-27 10:05

I\'m trying to check that dates entered by end users are in the YYYY-MM-DD. Regex has never been my strong point, I keep getting a false return value for the preg_match() I

相关标签:
23条回答
  • 2020-11-27 10:31

    yyyy-mm-dd : /^((((19|[2-9]\d)\d{2})\-(0[13578]|1[02])\-(0[1-9]|[12]\d|3[01]))|(((19|[2-9]\d)\d{2})\-(0[13456789]|1[012])\-(0[1-9]|[12]\d|30))|(((19|[2-9]\d)\d{2})\-02\-(0[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\-02\-29))$/g

    yyyy/mm/dd : /^((((19|[2-9]\d)\d{2})\/(0[13578]|1[02])\/(0[1-9]|[12]\d|3[01]))|(((19|[2-9]\d)\d{2})\/(0[13456789]|1[012])\/(0[1-9]|[12]\d|30))|(((19|[2-9]\d)\d{2})\/02\/(0[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))\/02\/29))$/g

    mm-dd-yyyy : /^(((0[13578]|1[02])\-(0[1-9]|[12]\d|3[01])\-((19|[2-9]\d)\d{2}))|((0[13456789]|1[012])\-(0[1-9]|[12]\d|30)\-((19|[2-9]\d)\d{2}))|(02\-(0[1-9]|1\d|2[0-8])\-((19|[2-9]\d)\d{2}))|(02\-29\-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g

    mm/dd/yyyy : /^(((0[13578]|1[02])\/(0[1-9]|[12]\d|3[01])\/((19|[2-9]\d)\d{2}))|((0[13456789]|1[012])\/(0[1-9]|[12]\d|30)\/((19|[2-9]\d)\d{2}))|(02\/(0[1-9]|1\d|2[0-8])\/((19|[2-9]\d)\d{2}))|(02\/29\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g

    dd/mm/yyyy : /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g

    dd-mm-yyyy : /^(((0[1-9]|[12]\d|3[01])\-(0[13578]|1[02])\-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\-(0[13456789]|1[012])\-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\-02\-((19|[2-9]\d)\d{2}))|(29\-02\-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g

    0 讨论(0)
  • 2020-11-27 10:33

    This method can be useful to validate date in PHP. Current method is for mm/dd/yyyy format. You have to update parameter sequence in checkdate as per your format and delimiter in explode .

        function isValidDate($dt)
        {
            $dtArr = explode('/', $dt);
            if (!empty($dtArr[0]) && !empty($dtArr[1]) && !empty($dtArr[2])) {
                return checkdate((int) $dtArr[0], (int) $dtArr[1], (int) $dtArr[2]);
            } else {
                return false;
            }
        }
    
    0 讨论(0)
  • 2020-11-27 10:35

    Format 1 : $format1 = "2012-12-31";

    Format 2 : $format2 = "31-12-2012";

    if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$format1)) {
        return true;
    } else {
        return false;
    }
    
    if (preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1])-(0[1-9]|1[0-2])-[0-9]{4}$/",$format2)) {
        return true;
    } else {
        return false;
    }
    
    0 讨论(0)
  • 2020-11-27 10:37

    From Laravel 5.7 and date format i.e.: 12/31/2019

    function checkDateFormat(string $date): bool
    {
        return preg_match("/^(0[1-9]|1[0-2])\/(0[1-9]|[1-2][0-9]|3[0-1])\/[0-9]{4}$/", $date);
    }
    
    0 讨论(0)
  • 2020-11-27 10:40

    If you want to match that type of date, use:

    preg_match("~^\d{4}-\d{2}-\d{2}$~", $date)
    
    0 讨论(0)
  • 2020-11-27 10:44

    Criteria:

    Every year divisible by 4 is a leap year, except when it is divisible by 100 unless it is divisible by 400. So:

    2004 - leap year - divisible by 4
    1900 - not a leap year - divisible by 4, but also divisible by 100
    2000 - leap year - divisible by 4, also divisible by 100, but divisible by 400
    

    February has 29 days in a leap year and 28 when not a leap year

    30 days in April, June, September and November

    31 days in January, March, May, July, August, October and December

    Test:

    The following dates should all pass validation:

    1976-02-29
    2000-02-29
    2004-02-29
    1999-01-31
    

    The following dates should all fail validation:

    2015-02-29
    2015-04-31
    1900-02-29
    1999-01-32
    2015-02-00
    

    Range:

    We'll test for dates from 1st Jan 1000 to 31st Dec 2999. Technically the currently used Gregorian calendar only came into use in 1753 for the British Empire and at various years in the 1600s for countries in Europe, but I'm not going to worry about that.

    Regex to test for a leap year:

    The years divisible by 400:

    1200|1600|2000|2400|2800
    can be shortened to:
    (1[26]|2[048])00
    
    if you wanted all years from 1AD to 9999 then this would do it:
    (0[48]|[13579][26]|[2468][048])00
    if you're happy with accepting 0000 as a valid year then it can be shortened:
    ([13579][26]|[02468][048])00
    

    The years divisible by 4:

    [12]\d([02468][048]|[13579][26])
    

    The years divisible by 100:

    [12]\d00
    

    Not divisible by 100:

    [12]\d([1-9]\d|\d[1-9])
    

    The years divisible by 100 but not by 400:

    ((1[1345789])|(2[1235679]))00
    

    Divisible by 4 but not by 100:

    [12]\d([2468][048]|[13579][26]|0[48])
    

    The leap years:

    divisible by 400 or (divisible by 4 and not divisible by 100)
    ((1[26]|2[048])00)|[12]\d([2468][048]|[13579][26]|0[48])
    

    Not divisible by 4:

    [12]\d([02468][1235679]|[13579][01345789])
    

    Not a leap year:

    Not divisible by 4 OR is divisible by 100 but not by 400
    ([12]\d([02468][1235679]|[13579][01345789]))|(((1[1345789])|(2[1235679]))00)
    

    Valid Month and day excluding February(MM-DD):

    ((01|03|05|07|08|10|12)-(0[1-9]|[12]\d|3[01]))|((04|06|09|11)-(0[1-9]|[12]\d|30))
    shortened to:
    ((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30))
    

    February with 28 days:

    02-(0[1-9]|1\d|2[0-8])
    

    February with 29 days:

    02-(0[1-9]|[12]\d)
    

    Valid date:

    (leap year followed by (valid month-day-excluding-february OR 29-day-february)) 
    OR
    (non leap year followed by (valid month-day-excluding-february OR 28-day-february))
    
    ((((1[26]|2[048])00)|[12]\d([2468][048]|[13579][26]|0[48]))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|[12]\d))))|((([12]\d([02468][1235679]|[13579][01345789]))|((1[1345789]|2[1235679])00))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|1\d|2[0-8]))))
    

    So there you have it a regex for dates between 1st Jan 1000 and 31st Dec 2999 in YYYY-MM-DD format.

    I suspect it can be shortened quite a bit, but I'll leave that up to somebody else.

    That will match all valid dates. If you want it to only be valid when it contains just one date and nothing else, then wrap it in ^( )$ like so:

    ^(((((1[26]|2[048])00)|[12]\d([2468][048]|[13579][26]|0[48]))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|[12]\d))))|((([12]\d([02468][1235679]|[13579][01345789]))|((1[1345789]|2[1235679])00))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|1\d|2[0-8])))))$
    

    If you want it for an optional date entry (ie. it can be blank or a valid date) then add ^$| at the beginning, like so:

    ^$|^(((((1[26]|2[048])00)|[12]\d([2468][048]|[13579][26]|0[48]))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|[12]\d))))|((([12]\d([02468][1235679]|[13579][01345789]))|((1[1345789]|2[1235679])00))-((((0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))|((0[469]|11)-(0[1-9]|[12]\d|30)))|(02-(0[1-9]|1\d|2[0-8])))))$
    
    0 讨论(0)
提交回复
热议问题