将日期作为搜索参数问题传递


Passing date as search parameter issue

当用户按日期搜索帖子时,我需要通过url将日期作为搜索参数。

喜欢:http://siteurl/post/index/created:03/15/2012

正如您所看到的,日期格式是mm/dd/yy,这会产生问题,因此我在请求中只得到03作为值。

编辑:我的参数中的一些代码进入控制器。

Array
(
[controller] => posts
[action] => index
[named] => Array
    (
        [created] => 03
    )
[pass] => Array
    (
        [0] => 15
        [1] => 2012
    )
[plugin] => 
[form] => Array
    (
    )
[url] => Array
    (
        [url] => /posts/index/created:03/15/2012
    )
[isAjax] => 
)

有没有一种方法可以让用户选择mm/dd/yy格式的日期,然后我可以在提交表格后更改为另一种格式?

您可以将日期转换为时间戳并使用传递

$date = '03/15/2012';
$timestamp = strtotime($date);

将其传递到url。

使用转换回"mm/dd/yy"

echo date('m/d/Y',$timestamp);

未命名的参数会传递给操作,因此您也可以在控制器中执行此操作:

function index($month, $day, $year)
{
}

如果您仍然想在不超过任何日期的情况下使用此操作,请使用此操作并测试是否给定了值:

function index($month = null, $day = null, $year = null)
{
}

编辑

如果它来自一个表单,您可能应该为表单使用POST而不是GET,并检索如下值:

function index()
{
    if(isset($this->data['Post']['created']))
    {
        $created = $this->data['Post']['created'];
        //Do you search here
    }
}

根据您的需要,可能有解决方案…;-)

您可以在将日期值作为URL的一部分进行传递时对其进行URL编码。处理请求的控制器操作需要使用urldecode将其转换回带有斜杠的原始日期值。