php check for a valid date, weird date conversions

后端 未结 10 751
盖世英雄少女心
盖世英雄少女心 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:18

    echo date('Y-m-d', strtotime($date));

    results in: "1999-11-30"

    The result of strtotime is 943920000 - this is the number of seconds, roughly, between the Unix epoch (base from which time is measured) to 1999-11-30.

    There is a documented mysql bug on mktime(), localtime(), strtotime() all returning this odd value when you try a pre-epoch time (including "0000-00-00 00:00:00"). There's some debate on the linked thread as to whether this is actually a bug:

    Since the time stamp is started from 1970, I don't think it supposed to work in anyways.

    Below is a function that I use for converting dateTimes such as the above to a timestamp for comparisons, etc, which may be of some use to you, for dates beyond "0000-00-00 00:00:00"

    /**
     * Converts strings of the format "YYYY-MM-DD HH:MM:SS" into php dates
     */
    function convert_date_string($date_string)
    {
        list($date, $time) = explode(" ", $date_string);
        list($hours, $minutes, $seconds) = explode(":", $time);
        list($year, $month, $day) = explode("-", $date);
        return mktime($hours, $minutes, $seconds, $month, $day, $year);
    }
    

提交回复
热议问题