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

dhorowitz1's avatar

Carbon\Carbon::now() giving error

When I'm in php artisan tinker and I'm trying to add a new article, I type in: $article = new App\Article(['title' => 'New', 'body' => 'new', 'published_at' => Carbon\Carbon::now()]); I get: InvalidArgumentException with message 'Trailing Data' But if I just type in Carbon\Carbon::now(); I get the expected array of date and time. Does anyone have any idea why this is happening all of the sudden? It wasn't like this yesterday.

0 likes
4 replies
dhorowitz1's avatar

Sure would be super helpful if someone could explain this to me. I can't figure out why it was just working and now it's not. It's when I'm trying to store an entry into the database.

dhorowitz1's avatar
dhorowitz1
OP
Best Answer
Level 1

Nevermind, I don't need Carbon\Carbon::now(), I can just use good ole PHP's date('Y-m-d'), works perfectly fine. I'd still like to know why Carbon decided to stop working for me and what the hell that exception means, but it seems this post has attracted VERY little interest.

Art's avatar

Hi maybe too late but still. I've investigated The Class to which i was passing date, and found out the following date format method in Article.php:

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

So it was expecting only date, but Carbon\Carbon::now(), which we're using, sends date plus time in the format: "2016-10-26 22:01:17.000000"

So i added "H:i:s" to the date format and it worked.

    public function setPublishedAtAttribute($date) {
             $this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d H:i:s', $date);
        }

Hope it helps to someone who gets stuck.

Please or to participate in this conversation.