Converting a Carbon Date to MySQL Timestamp

Save date to MySQL format by using Laravel Carbon

Using today's date as an example, the JQuery Datepicker format of dd-mm-yy sounds to me like it would display as 10-08-19.

But in your Carbon declaration, you are using d-m-Y, which expects a 4 digit year 10-08-2019.

Trying changing your input format to d-m-y to account for the 2 digit year.

$object->date = \Carbon::createFromFormat('d-m-y', $regdate)->format('Y-m-d');

More information on PHP date formatting:

https://www.php.net/manual/en/function.date.php

Edit: Other issues, plus changes made per comments.

public function store(Request $request)
{
$request->validate([
'reg_date' => 'required',
]);

// get the entire request as an array
$data = $request->all();

// correct the date format
$data['reg_date'] = \Carbon::createFromFormat('d-m-y', $data['reg_date'])->format('Y-m-d');

// create a new model
$model = ModelName::create($data);
}

Edit 2: Process multiple dates by keys.

$keys = ['reg_date','sell_date','purchase_date'];

foreach ($keys as $key) {
$data[$key] = \Carbon::createFromFormat('d-m-y', $data[$key])->format('Y-m-d');
}

carbon format date time with am to database datatime format

You would be better chaging for the format in your js so it's only a date time.

Having said that this will work:

//date
$date = "16/12/2017 16:53 PM";

//replace / with -
$date = str_replace('/','-',$date);

//remove PM and AM

$date = str_replace('PM','',$date);
$date = str_replace('AM','',$date);

//create a new instance of DateTime and pass in the date
$date = new \DateTime($date);

//print date format
echo $date->format('Y-m-d H:i:s');

Laravel - format date from Mysql database with carbon

It seems that you are using TIMESTAMP type so

->where('requested_at', '<=', now()->subDays(60)->startOfDay())->get();

As you can see there is no need to format Carbon instance.

Break it all down:

now() // Laravel helper to get Carbon::now()
->subDays(60) // subtract 60 days
->startOfDay() // set time part of timestamp to start of the day

Note: add requested_at into $dates array in your model for easier use

Converting a date to a carbon instance from query results

According to the Carbon API, you can create a Carbon instance like so:

$myDate = new Carbon($questionnaire->created_at);

And then just format it the way you want:

{{ $myDate->format('F d, Y h:ia') }}

It may not be exactly what you want, but it'll work.

P.S: your Questionnaire model has the created_at field inside the $dates attribute? I thought that was just enough, even accessing the data the way you are doing it now.

Changing format of column to datetime MySql

If you're using Laravel just use migrations... That'd be the cleanest way to do mass DB updates. Then update the records using their models, and use Carbon to handle the dates:

<?php

use App\TableModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UpdateColumnDatatype extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Get all the data to reformat it
$data = TableModel::all();

Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name'); // Removes the old column

// You need to set a ->default() or make this column ->nullable() or you'll
// get errors since the data is empty now.
$table->dateTime('column_name'); // Creates the new datetime
});

// Iterate over the old data to update the new rows
foreach($data as $model) {
$oldDate = Carbon::createFromFormat('d/m/Y h:i', $model->column_name);
$newDate = $oldDate->format('Y-m-d H:i:s'); // Format for DateTime

$obj = TableModel::find($model->id);
$obj->column_name = $newDate; // Change the record's data
$obj->save(); // Save the changes
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name'); // Removes the new column
$table->string('column_name'); // Creates the old column as a string (varchar equivalent)
// Do the same thing to turn the new datetime values back to your old format
// in case you decide to do a rollback
});
}
}

How to insert date format YY-MM into database?

@Ted Stresen-Reuter

Sorry for the late reply couldn't find a solution that works within the model. tried your method which i have tried before with minor changes.

thanks a lot for trying to help me.. i found a method to complete this inside the controller which i will not recommend, but this was the best i was able to find which works and i was on a tight schedule.

'date_from' => isset($date_from[$key]) ? date('Y-m-d h:i:s', strtotime($date_from[$key])) : '',
'date_to' => isset($date_to[$key]) ? date('Y-m-d h:i:s', strtotime($date_to[$key])) : '',

the type timestamp accepts at least dates in full formats. as i know how i pass the data from blade to the controller i was able to add a date method and within it a strttotime method to convert my YY-MM into YY-MM-DD and the date that is inserted by default will be 01.



Related Topics



Leave a reply



Submit