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:002018-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:002018-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-dTh:i', $request->date_start);
        $date2 = DateTime::createFromFormat('Y-m-dTh: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 (IlluminateDatabaseQueryException $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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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-dTH:i', $request->date_start);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x