php check for a valid date, weird date conversions

后端 未结 10 760
盖世英雄少女心
盖世英雄少女心 2020-12-10 04:13

Is there a way to check to see if a date/time is valid you would think these would be easy to check:

$date = \'0000-00-00\';
$time = \'00:00:00\';
$dateTime          


        
10条回答
  •  温柔的废话
    2020-12-10 04:25

    Don't expect coherent results when you're out of range:

    cf strtotime

    cf Gnu Calendar-date-items.html

    "For numeric months, the ISO 8601 format ‘year-month-day’ is allowed, where year is any positive number, month is a number between 01 and 12, and day is a number between 01 and 31. A leading zero must be present if a number is less than ten."

    So '0000-00-00' gives weird results, that's logical!


    "Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. %e, %T, %R and %D (there might be more) and dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems."

    cf strftime


    Use checkdate function instead (more robust):

    month: The month is between 1 and 12 inclusive.

    day: The day is within the allowed number of days for the given month. Leap year s are taken into consideration.

    year: The year is between 1 and 32767 inclusive.

提交回复
热议问题