Jane Doe Jane Doe - 1 month ago 13
PHP Question

Why is it that my input box shows Dec. 31, 1969?

It's supposed to be blank. How can I prevent this from happening?
I am using this code to show the date

public function bookedOn($chalet_id) {
$chalet = \App\Chalet::where('chalet_id', '=', $chalet_id)->get();
if ($chalet->count() > 0) {
$books = \App\EventBookings::where('chalet_id', '=', $chalet[0]->chalet_id)->value('created_at');
$str = date('M. d, Y',strtotime($books));
return $str;
} else {
return false;
}
}


I think there is something I need to do here:


$str = date('M. d, Y',strtotime($books));

Answer

Try like this:

public function bookedOn($chalet_id) {
    $chalet = \App\Chalet::where('chalet_id', '=', $chalet_id)->get();
    if ($chalet->count() > 0) {
        $books = \App\EventBookings::where('chalet_id', '=', $chalet[0]->chalet_id)->value('created_at');
        if ($books == '') {
            // some default date
            $books = '2010-01-01';
        }
        $str = date('M. d, Y',strtotime($books));
        return $str;
    } else {
        return false;
    }
}
Comments