How to convert date to datetime with PHP and Mysql

Asked

Viewed 58 times

-4

I am importing an Excel spreadsheet and need to convert the date to datetime for Mysql to accept saving. However, I have tried all the options on mutator of Laravel and I couldn’t make that conversion.

Code:

Controller:

$import = new ComercialImport

class ComercialImport implements
   ToCollection,
    WithHeadingRow,

public function collection(Collection $rows)
{
    /* acredito que teria que mudar aqui na array pois no model nao esta obedecendo, mas nao tenho ideia de como fazer aqui. */
    foreach ($rows as $row) {
        $com = Comercial::create([
       
           'vigencia_inicial'               => $row  ['vigencial_inicial'],
           'vigencial'                      => $row  ['vigencia_final'],

Model:

  class Comercial extends Model
     {

        protected $guarded = ['id'];

        public $timestamps = false;
        protected $dates = ['vigencia_inicial', 'Vigencia_final'];
        protected $dateFormat = 'd/m/Y';

Error:

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
  • i solved with protected $Dates . .

1 answer

-2

Try to use:

$data_inicio = Carbon Carbon::createFromFormat('Y-m-d H:i:s', $Dates->vigencia_inicial)->format(’d/m/Y H:i');
$data_fim = Carbon Carbon::createFromFormat('Y-m-d H:i:s', $Dates->Vigencia_final)->format(’d/m/Y H:i');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.