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

aleksov's avatar

Can't add date and time properly

I try to add date and time into my database but I have probems:

First: I create a form:


<div class="row">
<div class="form-group col-md-6">

    {!! Form::label('roba_spremna','The cargo is ready:') !!}
    {!! Form::text('roba_spremna', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group col-md-6">

    {!! Form::label('auction_end','Auction close at:') !!}
    {!! Form::text('auction_end', null, ['class'=>'form-control']) !!}
</div>
</div>

After that I add bootstrap-datetimepicker (js library):


$( document ).ready(function() {
                $(function () {
                    $('#roba_spremna, #auction_end').datetimepicker();
                });
            });

and at Article model I write:


 protected $fillable = [
        'title',
        'body',
        'roba_spremna',
        'auction_end'
    ];

    protected $dates = [
        'roba_spremna',
        'auction_end'
    ];

    public function setRobaSpremnaAttribute($date){
        $this->attributes['roba_spremna']= Carbon::createFromFormat('Y-m-d', $date);
    }

Now When I try to store date with time at my database so when I submit form I get this error: InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. Trailing data

http://i.imgur.com/V1imBwC.png

How I can solve my problem?

0 likes
5 replies
tykus's avatar

You should change the first argument in the Carbon::createFromFormat('Y-m-d', $date) to match whatever you are passing in.

// you're error is showing the datetime formatted as 12/30/2015 4:00PM so the matching pattern is:
Carbon::createFromFormat('m/d/Y h:i a', $date);
1 like
aleksov's avatar

@tykus_ikus no, dont work... I get:

InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. Unexpected data found. Trailing data

tykus's avatar

hmm... it doesn't look from the stack trace that there is extra whitespace, but just in case you should trim($date).

If this is still not working, die and dump $date in the setRobaSpremaAttribute() method and see what is being passed in there

aleksov's avatar

sorry ,the problem was becouse I dont add function setAuctionEndAttribute ...

Please or to participate in this conversation.