Date is inserting as 0000-00-00 00:00:00 in mysql

后端 未结 3 612
挽巷
挽巷 2021-01-13 14:57

My $date output is in the foreach loop

09/25/11, 02/13/11, 09/15/10, 06/11/10, 04/13/10, 04/13/10, 04/13/10, 09/24/09, 02/19/09, 12/21/

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 15:39

    I created my own function for this purpose, may be helpful to you:

    function getTimeForMysql($fromDate, $format = "d.m.y", $hms = null){
        if (!is_string($fromDate))  
            return null ;       
        try {
            $DT = DateTime::createFromFormat($format, trim($fromDate)) ;
        } catch (Exception $e) { return null ;}
    
        if ($DT instanceof DateTime){
            if (is_array($hms) && count($hms)===3)
                 $DT->setTime($hms[0],$hms[1],$hms[2]) ;
            return ($MySqlTime = $DT->format("Y-m-d H:i:s")) ? $MySqlTime : null ;
        }
        return null ;
    }
    

    So in your case, you use format m/d/yy :

    $sql_date = getTimeForMysql($date, "m/d/yy") ;
    if ($sql_date){
      //Ok, proceed your date is correct, string is returned.
    }
    

提交回复
热议问题