Convert the date format of an array with Laravel / PHP

0

How to convert index [date_start] to 2018-08-23 12:19 with php or laravel?

Array
    (
        [id_restaurant] => 303
        [date_start] => 23/08/2018 12:19
        [date_end] => 27/08/2018 12:19
    )
    
asked by anonymous 30.08.2018 / 19:16

2 answers

1

Yes, if you are bringing this data to a model (which is recommended), you can transform your database field into a carbon object, adding the column name into the $dates array of your model .

Example: $dates = ['date_start','date_end']

That way when you bring this data ( $model->find($id)->date_start ) it will be an instance of the class Carbon , and with that you use the format() method to play in the date / time template you want.

Example: $model->find($id)->date_start->format('Y-m-d h:i:s')

Reference

    
30.08.2018 / 19:26
1

At laravel I suggest you take a look at Carbon , there is a lot of material there, it is part of the mutators that already accompany laravel.

Or you can set a mutator as well (provided you are using the Date or DateTime types):

protected $casts = [
    'created_at' => 'datetime:Y-m-d h:i:s',
];
    
30.08.2018 / 19:26