Laravel "Unexpected data found" error when trying to change format of Carbon created_at date
Asked Answered
R

7

17

When I try to modify the format of the default created_at field of my Resource model, I get the following error:

{  
   "error":{  
      "type":"InvalidArgumentException",
      "message":"Unexpected data found.
                 Unexpected data found.
                 The separation symbol could not be found
                 Unexpected data found.
                 A two digit second could not be found",
      "file":"\/var\/www\/html\...vendor\/nesbot\/carbon\/src\/Carbon\/Carbon.php",
      "line":359
   }
}

Here is the code that produced the above error:

$tile = Resource::with('comments, ratings')->where('resources.id', '=', 1)->first();
$created_at = $tile->created_at;
$tile->created_at = $created_at->copy()->tz(Auth::user()->timezone)->format('F j, Y @ g:i A');

If I remove ->format('F j, Y @ g:i A') from the above code, it works fine, but it's not in the format I want. What could the problem be? I have almost identical code elsewhere in my app and it works without error.

UPDATE: Using setToStringFormat('F j, Y @ g:i A') does not cause an error, but returns null.

Rasberry answered 9/8, 2014 at 23:8 Comment(0)
R
14

Adding the following code to my model worked for me:

public function getCreatedAtAttribute($date)
{
    if(Auth::check())
        return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->copy()->tz(Auth::user()->timezone)->format('F j, Y @ g:i A');
    else
        return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->copy()->tz('America/Toronto')->format('F j, Y @ g:i A');
}

public function getUpdatedAtAttribute($date)
{
    return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('F j, Y @ g:i A');
}

This allows me to use created_at and updated_at in the format I want.

Rasberry answered 10/8, 2014 at 16:19 Comment(0)
S
6

I did experience the same problem, and in my search for an answer I stumled across How do you explain the result for a new \DateTime('0000-00-00 00:00:00')? .

I decided to change the datetime-columns in the database to nullable with default value = NULL, to prevent fields from having value '0000-00-00 00:00:00'.

My migration in laravel 5 looks like this:

Schema::table('table', function($table)
{
    $table->dateTime('created_at')->nullable()->default(null)->change();
    $table->dateTime('updated_at')->nullable()->default(null)->change();
});
Sevilla answered 22/6, 2015 at 12:35 Comment(0)
C
6

It's not a carbon issue, it's a conflict between a setAttribute or getAttribute in your model.

Creature answered 4/7, 2015 at 5:25 Comment(0)
O
3

You needed to parse given date before saving user this code

Carbon::parse($request->input('some_date'));
Oilcloth answered 10/3, 2020 at 10:0 Comment(0)
E
2

You shouldn't try to change the format of created_at. It has to be a Carbon object. If you want to display the created_at date in a different format, then just format it at the time you output it. Or you may want to create a method that changes the format so you can call it whenever you want it in a different format. For example, add a method like this to your Resource class:

public function createdAtInMyFormat()
{
   return $this->created_at->format('F j, Y @ g:i A');
}

You can also have that function adjust the timezone, etc. Then you can use $tile->createdAtInMyFormat() for example to get your special format created_at from your $tile object.

Emeraldemerge answered 10/8, 2014 at 0:11 Comment(1)
I see. Thanks. I added my own answer with code which worked better for me.Rasberry
B
1

first add this to your model:

protected $casts = [
    'start_time' => 'time:h:i A',
    'end_time' => 'time:h:i A',
];

and then you can further format it with either Carbon or other Php native functions, such as
date('h:i A', strtotime($class_time->start_time)) // "09:00 AM"

Burgoyne answered 16/2, 2021 at 6:31 Comment(0)
B
0

I ran into this issue, and it was simply a matter of uses dashes instead of slashes.

$model->update(['some_date' => '2020/1/1']); // bad
$model->update(['some_date' => '2020-1-1']); // good

Reminder: If you specify your dates on your model, Eloquent is smart enough to convert it for you.

protected $dates = [ 'some_date' ];
Bottomless answered 4/1, 2020 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.