URL中的解析日期并不总是可用(触发空白屏幕)

I'm trying to parse data (for filtering) from an URL. I want to be able to use http://domain.com/page.php?startDate=20150101&endDate=20150927. In this example, data is used/visible/calculated from 1 January till 27 September.

When the endDate is entered/changed, that date is properly used in a filter. If it's not entered todays date should be used (but that's where it goes wrong). For that I use the following code:

    $endDateFormat = DateTime::createFromFormat('Ymd', htmlspecialchars($query['endDate']));
    $endDate = $endDateFormat->format('Y-m-d'); // change YYYYMMDD to YYYY-MM-DD for MySQL.

    if (empty($endDate)) {
        $endDate = date('Y-m-d'); // Use todays date if date is not entered.
    } elseif ($endDate >= date('Y-m-d')) {
        $endDate = date('Y-m-d'); // Use todays date if date is newer than today.
    }

For an unknown reason the endDate must be added in the URL. If it isn't used, the page is blank. If entered, the code works as supposed: when you use 31-12-2015 as endDate it is automatically corrected to today (you cannot get data from the future, of course) and when you use a date earlier than today that date is used. I verified this by checking the calculations and simply an echo from $endDate.

I don't know how to proceed now to prevent a blank page. The purpose is to use todays date as endDate if you get at http://domain.com/page.php (and thus endDate is empty/NULL/not in the URL).

This issue also appears with the startDate, but I'm quite sure it has the same cause.

I've started from scratch (yet again) and found the solution that worked for me with less code. Not sure why I didn't find it sooner or what went wrong with the earlier 'restarts', but the following works as I want to:

        $endDate = htmlspecialchars($query['endDate']);
    if (empty($endDate)) {
        $endDate = date('Ymd');
    } elseif ($endDate >= date('Ymd')) {
        $endDate = date('Ymd');
    }