Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a carbon date to mysql timestamp.

Tags:

I have a timestamp variable column in a mysql database. Trying to convert a carbon timestamp to something that I can input there, but Carbon::now() only returns a Carbon object and when I try to use the timestamp string of the Carbon object, it does not register in mysql.

public function store(CreateArticleRequest $request){         $input = $request->all();          var_dump($input); // JUST SO YOU CAN SEE         $input['published_at'] = Carbon::now();          var_dump($input); // JUST SO YOU CAN SEE         Article::create($input);    } 

My first var dump is like so:

array (size=4)   '_token' => string 'Wy67a4hWxrnfiGz61wmXfYCSjAdldv26wOJiLWNc' (length=40)   'title' => string 'ASDFasdf' (length=8)   'body' => string 'asdfasdf' (length=8)   'published_at' => string '2015-08-26' (length=10)   

My second var dump is like so.

The mysql column relating to "published_at" is a timestamp variable. How an I suppose to convert this from a Carbon Object?

Thanks in advance.

like image 986
Alexander Kleinhans Avatar asked Aug 26 '15 07:08

Alexander Kleinhans


People also ask

How do I create a timestamp in MySQL?

Timestamp supports Universal Time Coordinated(UTC) for MySQL. The Timestamp is written in a format that is set at 19 characters: YYYY-MM-DD HH:MM: SS. The values for Timestamp data type ranges from date 1st January 1970 UTC to 19th January 2038 UTC.

How do you convert timestamps to carbon?

$timestamp = \Carbon\Carbon::createFromFormat('Y/m/d', '2016/05/05')->timestamp; EDIT: You changed your date format in your post while I was replying.. but even so.. use createFromFormat to ensure the proper order of month, day, year is used by carbon. If a date with no time on it, why not just use a date field?

What is difference between timestamp and datetime in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

How is timestamp stored in MySQL?

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME, which is stored “as is”.) By default, the current time zone for each connection is the server's time.


2 Answers

The short answer is that toDateTimeString() is what you're looking for:

$input['published_at'] = Carbon::now()->toDateTimeString(); 

See http://carbon.nesbot.com/docs/ for more options, including toDateString() if you just want the date part and not the time.

But an even better way to handle it would be to let Laravel handle casting the date value to/from a Carbon object for you. See https://laravel.com/docs/5.4/eloquent-mutators#date-mutators.

like image 92
orrd Avatar answered Sep 18 '22 03:09

orrd


The problem is in your date string, for example, you have this:

public function setCrbDateAttribute($value) {      $this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d-m-Y h:i', $value); } 

Now, if there is a date like 10-12-2014 then this error will occur because the hour and minute is missing. So you make sure that the date contains all the pars and also make sure that the date string contains - as a separator not /.

In other words, check the $value before you use Carbon and make sure your date string contains exactly the same formatted string you've used in the method.

This also happens in an accessor method, so check the date value first before you use it in Carbon::createFromFormat().

If you are getting the date from user input then validate the date before using it using date or date_format:format rule, check the validation here.

Answer ref:

Laravel/Carbon Timestamp 0000-00-00 00:00:00 or Unexpected data found. Unexpected data found. Data missing

like image 39
Vision Coderz Avatar answered Sep 22 '22 03:09

Vision Coderz