在选择框中按日期和时间搜索

时间:2013-03-07 15:05:33

标签: php date time

嗨,我有一张桌子,我希望随时间搜索以前的记录 我有搜索选项的选择框这里是代码

    if (isset($_REQUEST['searchtype']) && isset($_REQUEST['period']))
    {
    if ($_REQUEST['searchtype'] == "All")
        {
        $period = "All";
        }
    elseif ($_REQUEST['searchtype'] == "Last24hours")
        {
        $period = date('Y-m-d H:i:s')-86400;
        }
    elseif ($_REQUEST['searchtype'] == "Last10days")
        {
        $period = date('Y-m-d H:i:s')-864000;
        }
    elseif ($_REQUEST['searchtype'] == "LastHour")
        {
        $period = date('Y-m-d H:i:s')-3600;
        }
    elseif ($_REQUEST['searchtype'] == "Lastweek")
        {
        $period = date('Y-m-d H:i:s')-604800;
        }
    elseif ($_REQUEST['searchtype'] == "LastMonth")
        {
        $period = date('Y-m-d H:i:s')-2592000;
        }
      else
        {
        $period = $_REQUEST['searchtype'];
        }

我该怎么做......

2 个答案:

答案 0 :(得分:0)

像这样使用$period = time() - 86400;

答案 1 :(得分:0)

if (isset($_REQUEST['searchtype']) && isset($_REQUEST['period']))
{
    $searchtype = $_REQUEST['searchtype'];
    switch($searchtype)
    {
        case 'Last24hours':
            $period = time() - 86400;
        break;
        case 'Last10days':
            $period = time() - 864000;
        break;
        case 'LastHour':
            $period = time() - 3600;
        break;
        case 'Lastweek':
            $period = time() - 604800;
        break;
        case 'LastMonth':
            $period = time() - 2592000;
        break;
        case 'ALL':
        default:
            $period = "All";
    }

    $sql_by_time = ($period != "All") ? " AND time > $period" : "";
}

根据您在SELECT * FROM

中使用的内容替换时间
SELECT * FROM YOUR_TABLE
WHERE YOUR_WHERE
$sql_by_time