When passing dates from a form to be saved in your database you sometimes need to change their format.

For example a with a datetimepicker you want the format to look like 07-31-2017 12:30pm for the end user.  But in our database we need to store these as datetime 2017-07-31 12:30:00.

You can accomplish this easily and format just before the insert.

Take this simple code for example:

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');
    }

The departing and returning fields are both datetime fields in the database.  So you can use php strtotime formatting commands to update it.

public function store(){

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

        return $trip;

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

Leave a Reply