Converting a carbon date to mysql timestamp.

Asked
Active3 hr before
Viewed126 times

7 Answers

converting
90%

Find centralized, trusted content and collaborate around the technologies you use most.,The mysql column relating to "published_at" is a timestamp variable. How an I suppose to convert this from a Carbon Object? ,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.,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.

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

$input['published_at'] = Carbon::now() - > toDateTimeString();
load more v
88%

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. ,or you can just convert the date to timestamp using strtotime,The mysql column relating to "published_at" is a timestamp variable. How an I suppose to convert this from a Carbon Object? ,I'm trying to get all records that belongs to last month, so far I managed to get all from last month but to date today, I'm not sure how I can get only for las...

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);
}
load more v
72%

array (size=2) 0 => string '2015-01-31 11:59:00' (length=19) 1 => string '2015-01-31 15:59:00' (length=19),Grinding my teeth on this one. var_dumping $getDates from my sql query I get:, Sorry, something went wrong. , Trending →

$getDates = array('2015-01-31 11:59:00', '2015-01-31 15:59:00');

foreach($getDates as $i => $d) {
   $getDates[$i] = Carbon::parse($getDates[$i]);
}

var_dump($getDates);
65%

Carbon::createFromFormat('Y-m-d H:i:s', $request - > date) - > format('d-m-Y')
load more v
75%

MySQL doesn’t like that format, so we need to convert it to the correct date time format so MYSQL can store it.,To do it, we use Carbon. Carbon comes with Laravel which is awesome, so we can just import Carbon into our controller and make it work.,However, when sending the date to the database in Laravel, it posts as ISO8601 format (eg. 1996-10-15T00:05:32.000Z), Add featured image to admin table in WordPress for custom post type

use Illuminate\ Support\ Carbon;
use App\ Model;

public
function save(Request $request) {

   $model = new Model;
   $model - > date = Carbon::parse($request - > date) - > setTimezone('Australia/Sydney') - > format('Y-m-d h:i:s');
   $model - > save();
}
40%

Converting a datetime into something readable.,Consider the following example with a time in the future and a time in the past:,Consider the following example with a date in the future and a date in the past:,Displaying time relatively can sometimes be more useful to readers than a date or timestamp.

Whenever we need to use Carbon, we can import it like so:

< ? php
use Carbon\ Carbon;
load more v
22%

In this case, you need to convert the date within the query, if mysql is being used as your database then you may use a raw query like this:,timestamp columns like created_at are being parsed first. the U is simply the timestamp format. you can return your own format. for other formats see date docs.,The better way to manage dates with Laravel IMHO is to use the getDates accessor that is build into Laravel.,It is what Laravel does by default to created_at and updated_at.

edit: as stated in my comment, getDateFormat() is for both ways (insert, selects). your best bet would be using format inside the model. example:

public
function getMyBirthdayAttribute() {
   return $this - > my_birthday - > format('d.m.Y');
}
load more v

Other "converting-undefined" queries related to "Converting a carbon date to mysql timestamp."