user10408737
user10408737

Reputation:

Call to a member function format() on boolean in PHP LARAVEL

My question possibly may be a duplicate from this question:

PHP Fatal error: Call to a member function format() on boolean

But I'm sure that my question is not a duplicate.

I have a problem with try to save records in datetime format.

The error is the following:

Symfony \ Component \ Debug \ Exception \

FatalThrowableError (E_ERROR)

Call to a member function format() on boolean

1° record

Columns date_start and date_end respectively

court_id = 1 - NAME= TENNIS

2018-10-05 10:00:00 - 2018-10-05 11:00:00 works perfectly. It's save without problems

Then I try to save another record in my table.

The same columns respectively.

2° Record

court_id = 1 - NAME = TENNIS

2018-10-05 12:00:00 - 2018-10-05 13:00:00 not work, but it's work if the day is different for example 2018-10-06.

The dates are similar and court_id are the same, the only difference between the record 1 and 2 are the hours.

This is my function store()

public function store(Request $request){


    $hours = new HoursNew();

    try {


        $hours->id = $request->id;

        $date = DateTime::createFromFormat('Y-m-d\Th:i', $request->date_start);
        $date2 = DateTime::createFromFormat('Y-m-d\Th:i', $request->date_end);


        //THIS LINE SHOW ME AS AN ERROR
        $hours->date_start = $date->format('Y-m-d H:i:s');


        $hours->date_end = $date2->format('Y-m-d H:i:s');


        $hours->estate_hour_id = $request->estate_hour_id;
        $hours->court_id = $request->court_id;
        $hours->save();

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

         Session::flash('error', 'Whoops! We have some problems');

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

    }
        Session::flash('message', "It's OK");
        return redirect()->route('ListHours.store');
}

Why I get this error, when I try to save the same dates for the same courts (court_id) but differents hours.

Upvotes: 0

Views: 8120

Answers (1)

Chris Ruskai
Chris Ruskai

Reputation: 459

http://php.net/manual/en/datetime.createfromformat.php

DateTime::createFromFormat() returns false on failure. Since the hour goes from <= 12 to 13 in your example, that indicates to me that the "h" should be capitalized.

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

Upvotes: 1

Related Questions