Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bhhussain's avatar

Data format issue in Mysql

Hi,

I have date format (dd/mm/yyyy) and when I try to insert into table i am getting error message as below.

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value

Default date format of mysql is yyyy/mm/dd hh:mm and I want to convert my date format into mysql format to insert the data.

I am using the bootstrap date picker as below

$( ".datepicker" ).datepicker({ format: 'dd-mm-yyyy' });

Can any one please help me.

Thank you

0 likes
19 replies
rameezisrar's avatar

@bhhussain try this

$('.date-picker').datepicker({
                format: 'yy/mm/dd',
                autoclose: true,
                todayHighlight: true
            });
bobbybouwmann's avatar

Well, before you insert it in the database you need to convert it so it's in the correct format. You can do the conversion yourself in either the frontend using momentjs or using Carbon in your PHP backend.

Using Carbon it will be handled automatically for you:

$date = Carbon::createFromFormat('dd/mm/yyyy', $date);
1 like
bhhussain's avatar

This code is not working for me.

Is any other way?

Thank you

Sinnbeck's avatar

Can you show your current code that does not work? Both solutions should work :)

munazzil's avatar

Use as like below and check,

 $date  = Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
bhhussain's avatar

I added use Carbon\Carbon; in the top and below code

public function store(Request $request, Account $account, Item $item) {

    $id = Account::orderByDesc('th_tran_no')->first()->th_tran_no ?? date('Y') . 00000;
    $year = date('Y');
    $id_year = substr($id, 0, 4);
    $seq = $year <> $id_year ? 0 : +substr($id, -5);
    $new_id = sprintf("%0+4u%0+6u", $year, $seq+1);      


    if($request->th_attach->getClientOriginalName())
    {
        $ext = $request->th_attach->getClientOriginalExtension();
        $file = date('YmdHis').rand(1,99999).'.'.$ext;
        $request->th_attach->storeAs('public/categories',$file);
    }    
    else
    {
        $file='';
    }
    $account->th_attach = $file;
    $account->th_supp_name = $request->th_supp_name;
    $account->th_emp_name = $request->th_emp_name;
    $account->th_item_type = $request->th_item_type;
    
    $date  = Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $th_bill_dt)->format('Y-m-d');
    
    $account->th_bill_dt = $date;     


    $account->th_bill_amt = $request->th_bill_amt;
    $account->th_bill_no = $request->th_bill_no;
    $account->th_pay_mode = $request->th_pay_mode;
    $account->th_purpose = $request->th_purpose;
    $account->th_item_type = $request->th_item_type;
    $account->th_tran_no = $new_id;
    $account->th_emp_name = Auth::user()->name;
    
    $account->save();       
    
    foreach ($request->td_item_desc as $key =>$name) 
    {
        $item = resolve(Item::class);     
        $item->td_item_desc = trim($name,'"');    
        $item->td_qty = $request->td_qty[$key] ;    
        $item->td_unit_price = $request->td_unit_price[$key] ;
        $item->td_tran_no = $new_id; 
        $item->save();
    }         
    return redirect('admin/accounts')->with('success','Transaction created successfully!');
}
bhhussain's avatar

I am getting this error message.

Class 'Carbon\Carbon\Carbon' not found

When I add the name space (namespace Carbon;) in this controller page then different error message is coming.

Sinnbeck's avatar

As you are already importing Carbon it should just be

 $date  = Carbon::createFromFormat('Y-m-d H:i:s', $th_bill_dt);
Snapey's avatar

dump the request as it hits your server. Check the format of the date string.

Use Carbon::createFromFormat() to create a Carbon object using the format of the passed date

Pass the Carbon date time object to Eloquent. It will know how to use that Carbon object to update your database.

bhhussain's avatar

Sorry to bother all of you for small issue..

Still I have the problem..and error says

InvalidArgumentException Data missing

when I use the below code

$bill_dt = $request->th_bill_dt; $newdate = Carbon::createFromFormat('Y-m-d H:i:s', $bill_dt); $account->th_bill_dt = $newdate;

and this error message

ErrorException Undefined variable: th_bill_dt

when I use

$newdate = Carbon::createFromFormat('Y-m-d H:i:s', $th_bill_dt); $account->th_bill_dt = $newdate;

Really I am new and learning larvel.. that is the reason I am struggling in basic.. I do apologize

Sinnbeck's avatar

You dont have a variable called $th_bill_dt

Which variable should hold the date variable? Replace $the_correct_variable_name

 $date  = Carbon::createFromFormat('Y-m-d H:i:s', $the_correct_variable_name);
Snapey's avatar

your missing data is because you are trying to parse a full date time string when you only have date (thanks for the mis-advice again @munazzil )

dd(request->all())

at the top of the method and check what date format is being sent

bhhussain's avatar

@sinnbeck This code is also not working.

I am trying like this

$date = Carbon::createFromFormat('Y-m-d', $request->th_bill_dt); $account->th_bill_dt = $date;

Error Message:

InvalidArgumentException Trailing data

bhhussain's avatar

sorry


$date  = Carbon::createFromFormat('Y-m-d', $request->th_bill_dt);        
$account->th_bill_dt = $date;   

Snapey's avatar

HAVE YOU CHECKED WHAT DATE FORMAT COMES FROM YOUR FORM ?

otherwise you are just stumbling around

Nakov's avatar

@bhhussain can you show here the result of this:

dd($request->th_bill_dt);

Before you try to create a carbon instance from it?

Nakov's avatar
Nakov
Best Answer
Level 73

@bhhussain Actually I saw it now in your datepicker :)

so you should use this instead:

$date  = Carbon::createFromFormat('d-m-Y', $request->th_bill_dt);        

not vice-versa :)

Please or to participate in this conversation.