Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to INSERT date in PHP & Mysqli

i have variable $est_date = "25-02-2015" this is DD-MM-YYYY format so now i need to insert this value in mysql table, the column type is DATE TIME, so i tried like this but the date are not storing in the coloumn est_date, it just showing me 00-00-0000

$mysqli = new mysqli('localhost','root','','smart_marine');
$insertdate = date("d-m-Y", strtotime($est_date));
$insert = $mysqli->prepare("INSERT into repair ( 
                                            cont_details_id,
                                            depot_details_id, 
                                            cont_condition,
                                            est_cost,
                                            currency,
                                            est_date, 
                                            location
                                            )
                                VALUES (?,?,?,?,?,?,?)");

        $phpArray = json_decode($serialize_data, true);
        foreach ($phpArray as $key => $value)
        { 
            if($value['rate']!=''&& $value['drop']!='')
            {       
            $insert->bind_param('sssssss',
                                 $value['id'], 
                                 $depot_details_id, 
                                 $value['drop'], 
                                 $value['rate'],
                                 $currency,
                                 $insertdate, 
                                 $location);                                            
            $insert->execute();
            }
        }

please some one help me.

i tried to store like this too

$insert->bind_param('sssssss',
                                     $value['id'], 
                                     $depot_details_id, 
                                     $value['drop'], 
                                     $value['rate'],
                                     $currency,
                                    STR_TO_DATE($est_date , '%d-%m-%Y'), 
                                     $location);        

but this return error "undefined function STR_TO_DATE"
like image 863
Munna Babu Avatar asked Jan 09 '23 05:01

Munna Babu


1 Answers

You need to change this line:-

$insertdate = date("d-m-Y", strtotime($est_date));

to

$insertdate = date("Y-m-d", strtotime($est_date));

Note:- DATETIME data-type format is Y-m-d H:i:s. thanks.

like image 84
Anant Kumar Singh Avatar answered Jan 13 '23 20:01

Anant Kumar Singh