Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

Change Date Before DB Insert - Laravel

I'm trying to figure out what the correct way is to modify a date format before inserting when using Laravel 5.4.

I have:
public function store(){

        $trip = new Trip;
        $trip->tripName = request('tripName');
        $trip->departing = request('departing');
        $trip->returning = request('returning');
        $trip->save();

        return $trip;

        //return redirect('/home');
    }

Open in new window


My database mySQL field in datetime, but the date that's passed in the variable is '07/17/2017 3:20 PM'.  How/where would I update that before insertion?
Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

ASKER

I was able to accomplish like this, although not sure if correct way to do in Laravel 5.4?

public function store(){
        $trip = new Trip;
        $trip->tripName = request('tripName');
        $trip->departing = date('Y-m-d H:i:s', strtotime(request('departing')));
        $trip->returning = date('Y-m-d H:i:s', strtotime(request('returning')));
        $trip->save();

        return $trip;
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial