【问题标题】:Filter Date using Date Picker in Yii在 Yii 中使用日期选择器过滤日期
【发布时间】:2012-06-06 06:37:00
【问题描述】:

我使用 gii 生成了我的 crud 屏幕。我有一个搜索表单,我在其中放置了一个日期选择器,我让用户选择他想要的日期。

但问题是我在数据库中以秒为单位存储了日期。

我知道我可以使用 strtotime 转换日期。但是如何在我的模型中使用搜索方法进行过滤?

这是我的日期选择器

<?php 
        $this->widget('zii.widgets.jui.CJuiDatePicker', array(

        'name'=>'ordering_date',
        'id'=>'ordering_date',
        // additional javascript options for the date picker plugin
        'options'=>array(
            'showAnim'=>'fold',
        ),
        'htmlOptions'=>array(
            'style'=>'height:20px;'
        ),
        ));
    ?>

这是我在模型中的搜索方法。我想比较ordering_date

public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.
    //echo $this->ordering_date;
    $criteria=new CDbCriteria;

    $criteria->compare('order_id',$this->order_id);
    $criteria->compare('customer_id',$this->customer_id);
    $criteria->compare('delivery_address_id',$this->delivery_address_id);
    $criteria->compare('billing_address_id',$this->billing_address_id);
    $criteria->compare('ordering_date',$this->ordering_date);
    $criteria->compare('ordering_done',$this->ordering_done,true);
    $criteria->compare('ordering_confirmed',$this->ordering_confirmed);
    $criteria->compare('payment_method',$this->payment_method);
    $criteria->compare('shipping_method',$this->shipping_method);
    $criteria->compare('comment',$this->comment,true);
    $criteria->compare('status',$this->status,true);
    $criteria->compare('total_price',$this->total_price);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

【问题讨论】:

    标签: php yii


    【解决方案1】:

    试试这个:

    $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'model'=>$model,
            'attribute'=>'ordering_date',
            'options'   => array('dateFormat' => 'mm-dd-yy',),
    

    然后搜索

    $begindate = CDateTimeParser::parse($this->ordering_date, 'MM-dd-yyyy');
    $enddate   = $begindate + 24* 60*60;
    $criteria->addBetweenCondition('ordering_date', $begindate, $enddate);
    

    【讨论】:

    • $this-&gt;ordering_date 从输入中获取它的值,因此它已经是mm-dd-yyy 格式。
    【解决方案2】:

    在比较之前先进行$this-&gt;ordering_date = strtotime($this-&gt;ordering_date);$criteria-&gt;compare('ordering_date', strtotime($this-&gt;ordering_date));

    【讨论】:

    • 是的,我做到了!我没有得到正确的结果。然后我发现,当您转换从日期选择器获得的日期并与数据库值进行比较时,它会给出不同的值,因为当我存储订单日期时,我也会考虑分钟和秒。因此,我尝试向条件添加条件以获取所选日期和下一个日期之间的值。但是,当您加载屏幕时,最初没有给出任何结果。有什么想法吗?
    • 为什么要存储分钟/秒,如果这只会使事情复杂化,而且您甚至不能按它们过滤?只需使用 00 表示小时/分钟/秒。
    猜你喜欢
    • 2015-06-22
    • 1970-01-01
    • 2010-09-19
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多