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

TKay's avatar
Level 1

Laravel timstamp set from Carbon::parse()

Hi Everyone!

I have a db column 'expired_at' which is migrated usin $table->timstamp('expired_at')

My form input of date for is March 26, 2016

I am setting my db column value using

[ ... 'expired_at' => Carbon::parse($inputs['expired_at'])->toDateTimeString(); ... ]

But it inserts timestamp in db 0000-00-00 00:00:00

How can I insert timestamp using Carbon::parse() ?

0 likes
5 replies
ARCANEDEV's avatar
  • Try to dd() the Carbon::parse($inputs['expired_at'])->toDateTimeString() and see what you got, check the http://carbon.nesbot.com/docs/ for more details.
  • Check that the expired_at is in your Model fillable property.
1 like
Snapey's avatar
Snapey
Best Answer
Level 122

I would use Carbon::createFromFormat('M d, Y', $inputs['expired_at'])->toDateTimeString();

If you want to just pass the carbon object to eloquent rather than converting it back to a string, add it to the $dates array in your model

protected $dates = ['created_at', 'updated_at', 'expired_at'];

this means that you also automatically get back a Carbon object and can format it however you need in your views etc.

TKay's avatar
Level 1

I have added in my model ...

        protected $dates = ['created_at', 'updated_at', 'expired_at'];

        protected $fillable= [ 'expired_at'];

I am inserting into database from controller like this ...

    $announcement = Announcement::create([
                'title' => $inputs['title'],
                'source_url' => $inputs['source_url'],
                'has_source_url' => $request->input('has_source_url') == "checked" ? 1 : 0,
             'expired_at' => Carbon::createFromFormat('M d, Y', $inputs['expired_at'])->toDateTimeString()
        ]);

Input field date format is "March 16, 2016"

db connection: mysql

migration script is :

    Schema::create('announcements', function (Blueprint $table) {
                    ...
                        $table->date('expired_at')->default(Carbon::now()->subWeeks(1))->comment('The data and time when the announcement will dissapear from billboard');
            ....
                        $table->timestamps();
         });

still has the problem

Snapey's avatar

Now you have the expired_at in your dates field you should be able to drop the toDateTimeString part.

    $announcement = Announcement::create([
        'title' => $inputs['title'],
        'source_url' => $inputs['source_url'],
        'has_source_url' => $request->input('has_source_url') == "checked" ? 1 : 0,
        'expired_at' => Carbon::createFromFormat('M d, Y', $inputs['expired_at'])
    ]);
1 like
TKay's avatar
Level 1

SOLVED!

My input format was "Tue May 31 2016 00:00:00 GMT+0900 (Korea Standard Time)" which I was mistaken.

Carbon error showed "Trailing Data" - because the string trailing " (Korea Standard Time)" is not a data format to parse. So parsing was failed. I have used PHP preg_replace() to remove the trailing Time zone elaboration.

    $input_text_expired_at = preg_replace("/\s(\(\w.+)/", "", $inputs['expired_at']);

Mr. @Snapey ... Thank you. You are right. For above format I have used

    'expired_at' => Carbon::createFromFormat('D M d Y g:i:s P', $inputs['expired_at'])->toDateTimeString()

Now my input date format is saving

Please or to participate in this conversation.