php check for a valid date, weird date conversions

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

    If you just want to handle a date conversion without the time for a mysql date field, you can modify this great code as I did. On my version of PHP without performing this function I get "0000-00-00" every time. Annoying.

    function ConvertDateString ($DateString)
    {
        list($year, $month, $day) = explode("-", $DateString);
        return date ("Y-m-d, mktime (0, 0, 0, $month, $day, $year));
    }
    
    0 讨论(0)
  • 2020-12-10 04:17

    I have been just changing the martin answer above, which will validate any type of date and return in the format you like.

    Just change the format by editing below line of script strftime("10-10-2012", strtotime($dt));

    <?php
    echo is_date("13/04/10");
    
    function is_date( $str ) {
        $flag = strpos($str, '/');
    
        if(intval($flag)<=0){
            $stamp = strtotime( $str );
        } else {
            list($d, $m, $y) = explode('/', $str);    
            $stamp = strtotime("$d-$m-$y");
        } 
        //var_dump($stamp) ;
    
        if (!is_numeric($stamp)) {
            //echo "ho" ;
            return "not a date" ;        
        }
    
        $month = date( 'n', $stamp ); // use n to get date in correct format
        $day   = date( 'd', $stamp );
        $year  = date( 'Y', $stamp );
    
        if (checkdate($month, $day, $year)) {
            $dt = "$year-$month-$day" ;
            return strftime("%d-%b-%Y", strtotime($dt));
            //return TRUE;
        } else {
            return "not a date" ;
        }
    }
    ?>
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-10 04:21

    This version allows for the field to be empty, has dates in mm/dd/yy or mm/dd/yyyy format, allow for single digit hours, adds optional am/pm, and corrects some subtle flaws in the time match.

    Still allows some pathological times like '23:14 AM'.

    function isValidDateTime($dateTime) {
        if (trim($dateTime) == '') {
            return true;
        }
        if (preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})(\s+(([01]?[0-9])|(2[0-3]))(:[0-5][0-9]){0,2}(\s+(am|pm))?)?$/i', $dateTime, $matches)) {
            list($all,$mm,$dd,$year) = $matches;
            if ($year <= 99) {
                $year += 2000;
            }
            return checkdate($mm, $dd, $year);
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-10 04:22
    <?php
    
    function is_date( $str ) {
        $stamp = strtotime( $str );
    
        if (!is_numeric($stamp)) {
            return FALSE;
        }
        $month = date( 'm', $stamp );
        $day   = date( 'd', $stamp );
        $year  = date( 'Y', $stamp );
    
        if (checkdate($month, $day, $year)) {
            return TRUE;
        }
        return FALSE;
    }
    ?>
    
    0 讨论(0)
  • 2020-12-10 04:24

    I have used the following code to validate dates coming from ExtJS applications.

    function check_sql_date_format($date) {
        $date = substr($date, 0, 10);
        list($year, $month, $day) = explode('-', $date);
        if (!is_numeric($year) || !is_numeric($month) || !is_numeric($day)) {
            return false;
        }
        return checkdate($month, $day, $year);
    }
    
    0 讨论(0)
提交回复
热议问题