可以格式化没有日期的日期

I usually use this to format dates:

$date = DateTime::createFromFormat('Y-m-d\TH:i', $request->id_input);

Is it possible to format a date without the date; only the time? I had tried this, from Manual PHP DateTime::createFromFormat:

$hour = DateTime::createFromFormat('H\hi\m \s', $request->sel)->format('H:i:s');

This doesn't work. It throws an error that says:

 Symfony\Component\Debug\Exception\

 FatalThrowableError (E_ERROR)

 Call to a member function format() on boolean

I have a select with some options as in the following example:

<select name="sel">
  <option value="08:00:00">08:00:00</option>
  <option value="09:00:00">09:00:00</option>
  <option value="10:00:00">10:00:00</option>
</select>

I have a select to select the time and the date is in another input.

Finally, I try to save the record into my database in MySQL. This is my controller:

public function store(Request $request){

    $schedules = new SchedulesNew();

    try {

        $schedules->id = $request->id;

        $schedules->court_id = $request->court_checkbox;

        /*___________________________________________________________*/

        $hour = DateTime::createFromFormat('H\hi\m \s', $request->sel)->format('H:i:s');
        $schedules->start_time =$request->date_input + $hour;

        $hour2 = DateTime::createFromFormat('H\h i\m s\s', $request->sel2)->format('H:i:s');
        $schedules->end_time = $request->date_input_end + $hour2;

        $schedules->state_schedule_id = $request->state_schedule_id;

        $schedules->save();



    } catch (\Illuminate\Database\QueryException $e) {

         Session::flash('error', 'There was an unknown error');

         return redirect()->route('schedules.store');

    }

        Session::flash('message', "The record was saved successfully");
        return redirect()->route('schedules.store');

}

If I don't convert the time that I received from the select, my website shows me an error that says:

A non well formed numeric value encountered

On these lines:

 $schedules->start_time =$request->date_input + $request->sel;

 $schedules->end_time = $request->date_input_2 + $request->sel2;

That is the reason that I think I need to convert to a time format, not a date format.

As you are using Laravel, try with Carbon like this:

Carbon\Carbon::createFromFormat('H:i:s', "08:00:00");

this is the response

=> Carbon\Carbon {#1226
    "date": "2018-10-30 08:00:00.000000",
    "timezone_type": 3,
    "timezone": "UTC",
   }

You have to wrap the whole DateTime::createFromFormat() part of your code in parenthesis. Like this:

$hour = (DateTime::createFromFormat('H\hi\m \s', $request->sel))->format('H:i:s');