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

Viewed 4876

My booking pluging on wordpress is not working It say Fatal error: Call to a member function format () on boolean in ...

and say me to go to this line of archive

public static function convert_date_to_mdY( $date_string, $date_format = null ) {
    if ( ! $date_format ) {
        $date_format = AWE_function::get_current_date_format();
    }

    if ( 'm/d/Y' == $date_format ) {
        return $date_string;
    }

    $d = DateTime::createFromFormat( $date_format, $date_string );
    return $d->format( 'm/d/Y' );
}

What´s the problem?

2 Answers

It is possible that DateTime::createFromFormat() will be unable to create the DateTime if values thrown into it have different Format other than DateTime.

For example, if any TEXT or STRING is thrown into it as its values, then it will produce error.

Date is separated by '.' or by '-' or by '/'. Time is separated by ':'.

This constitutes basic FORMAT of DateTime.

Use IF condition to check whether NULL Values or TEXT values are given into it which are different from DATETIME FORMAT.

Also, type of variable should be checked. Also, it should be checked, if given variable is an object? Also, Format should be from following : -

w3schools -> func_date_date_format

With thanks, Sukumar

Related