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

ayekoto's avatar

Date picker error while saving to database, help needed

I have below in format in my model

    /**
     * format to date
     * 
     * @param due_date 
     */
    public function setDueDateAttributes($due_date)
    {       
        $this->attributes['due_date'] = Carbon::createFromFormat('d/m/Y', $due_date);
    }

and my code:

<div class="input-group date" id="datetimepicker2">
    <input type="text" name="due_date"  class="form-control">
    <span class="input-group-addon"><span class="glyphicon glyphicon-calender"></span</span>                            
 </div>

$("#datetimepicker2").datetimepicker({
        locale: "en-au"
 });

Everything works well not until i tried to use the datepicker pluggin because i need time to be set along with date

I got the error: InvalidArgumentException in Carbon.php line 425: Unexpected data found. Unexpected data found. A two digit minute could not be found Trailing data

0 likes
18 replies
ayekoto's avatar

I think the error is due to the format Carbon expect is not what am sending from the frontend but don't just know how to format this properly, battling with this for hours .

Please assists so i can move on

mehany's avatar

@ayekoto try

public function getDueDateAttribute($dueDate){
    try{
        $formatedDate = Carbon::parse($dueDate)->format('m-d-Y');
        return $formatedDate;
    }catch(Exception $e){
        return $e; 
    };
}

parse() is my favorite because it does not really care about the formate it receives.

ayekoto's avatar

@mehany , still not working even with Carbon::parse(due_date);

Any help!!!!!!

cristian9509's avatar
public function setDueDateAttributes($due_date)
    {       
        $this->attributes['due_date'] = Carbon::createFromFormat('d/m/Y', $due_date);
    }

What format is the $due_date variable when you pass it to Carbon? try to dump($due_date) and see what is happening there.

1 like
ayekoto's avatar

Here is what it output when i dump($due_date)

"20/01/2016 12:00 AM"
cristian9509's avatar

Well, I believe that the format must match `'d/m/Y' in order to be interpreted well. You have two options, try them both and see what happens:

  1. Extract from the string only the '20/01/2016' portion. So your $due_date = '20/01/2016';

or

  1. Modify your format used in the Carbon createFromFormat method to 'd/m/Y g:i A'

If I got the date format characters wrong you can check them here http://php.net/manual/en/function.date.php

Carbon documentation:

Carbon::createFromFormat($format, $time, $tz); createFromFormat() is mostly a wrapper for the base php function DateTime::createFromFormat. The difference being again >the $tz argument can be a DateTimeZone instance or a string timezone value. Also, if there are errors with the format this >function will call the DateTime::getLastErrors() method and then throw a InvalidArgumentException with the errors as the >message. If you look at the source for the createXX() functions above, they all make a call to createFromFormat().

echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00

ayekoto's avatar

@cristian9509 yea it, the format is right but how to modify my format in the Carbon createFromFormat,

been trying to acheive that in my Model.php like below but not getting it right

line 2861 Model.php
        else
        {
            $value = Carbon::createFromFormat($format, $value); //original format met 
            // $value = Carbon::createFromFormat($format, (new DateTime($value))->format('Y-m-d g:i a'));
            
        }

will appreciate you show how to modify in this senerio

cristian9509's avatar

try this for option 2

public function setDueDateAttributes($due_date)
    {       
        $this->attributes['due_date'] = Carbon::createFromFormat('d/m/Y g:i A', $due_date);
    }
ayekoto's avatar

Have tried that earlier as well but no way

ayekoto's avatar

when i did

    public function setDueDateAttributes($due_date)
    {
        $this->attributes['due_date'] = Carbon::createFromFormat('d/m/Y g:i A', $due_date);
    }

i get error:

    InvalidArgumentException in Carbon.php line 425:
    Unexpected data found.
    Unexpected data found.
    Unexpected data found.
    Trailing data
cristian9509's avatar

Check this http://stackoverflow.com/questions/25223958/laravel-unexpected-data-found-error-when-trying-to-change-format-of-carbon-cre

do something in your setDueDateAttribues

$this->attributes['due_date'] = Carbon::createFromFormat('d/m/Y g:i A', $due_date);
dd($this->attributes['due_date']);

If you are going to see what is dumped it means that you problem might be a conflict between your setter and getter. Also did you added your 'due_date' to the protected $dates array in your model?

protected $dates = ['due_date'];
ayekoto's avatar

yes i did :

protected $dates = ['due_date'];

and dd($this->attributes['due_date']); returns no result except the same error:

    InvalidArgumentException in Carbon.php line 425:
    Unexpected data found.
    Unexpected data found.
    Unexpected data found.
    Trailing data
nateritter's avatar

It seems Laravel does the Carbon date changes prior to Mutators being called, which means mutators won't solve the issue if a non-typical date string is sent through to get saved.

There's two possible solutions I've found:

(1) Before trying to save the data, do something like this:

$data = $request->all();
if ($request->has('notice_to_proceed_at')) {
        $data['notice_to_proceed_at'] = Carbon::createFromFormat('m/d/Y', $request->input('notice_to_proceed_at'));
}

... and then pass in $data to your save() method.

I'm not a fan of this. Just doesn't feel right, but it'll do the trick.

(2) Remove the field from your protected $dates array and create a Mutator and Accessor to get the power of Carbon again like so....

public function setNoticeToProceedAtAttribute($value)
{
    // Normalize the format
    $this->attributes['notice_to_proceed_at'] = date('Y-m-d', strtotime($value));
}

public function getNoticeToProceedAtAttribute($value)
{
    return Carbon::createFromFormat('Y-m-d', $value);
}

(in my case I have a date MySQL column, not datetime, which is why I assume the formatting is 'Y-m-d')

1 like
Mkennethsmith's avatar

solved my issue when using the vue-datepicker component, was sending through dates with time zone format made by moment js. Thanks mate

Please or to participate in this conversation.