user7432810
user7432810

Reputation: 695

Laravel: I can't insert datetime or timestamp inside mysql

I am trying to insert an string date-time format into database but it doesn't seem to be working. This is the time format:

 $created_at = "Fri Jun 30 09:38:29 +0000 2017";

And this is my Code:

 $tweet = \App\AdminModels\Tweet::updateOrCreate(
        ['twitter_id' => $result->id],
        ['pub_date' => $created_at,'text' => $text]
 );

The script successfully inserts/update a record but the 'pub_date' column remains zero depending on the data type I choose.

For example if I choose the datatype as timestamp and defalut NULL, it remains null.

If I select datetime as the datatype it remains: 0000-00-00 00:00:00

What am I doing wrong here?

Upvotes: 1

Views: 1640

Answers (2)

linktoahref
linktoahref

Reputation: 7972

Use Carbon to parse the date for datetime datatype

$created_at = "Fri Jun 30 09:38:29 +0000 2017";


$tweet = \App\AdminModels\Tweet::updateOrCreate(
        ['twitter_id' => $result->id],
        [
            'pub_date' => Carbon::parse($created_at)->toDateTimeString(),
            'text' => $text
        ]
 );

Upvotes: 1

Hariharan
Hariharan

Reputation: 1194

Mysql throw Warning: #1265 Data truncated for column error for your input "Fri Jun 30 09:38:29 +0000 2017".

If the field type timestamp,the input value format should be 2017-06-30 03:35:31 like this format 0000-00-00 00:00:00

Upvotes: 2

Related Questions