dsfdfdfd6576578 2015-09-27 15:10
浏览 31

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.

  • 写回答

1条回答 默认 最新

  • dongzhong1929 2015-09-27 16:28
    关注

    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');
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题