Yii CJuiDatePicker不同的日期格式

时间:2013-08-30 12:45:47

标签: php yii datepicker

所以我想在搜索中的选择字段中显示日期为dd / mm / yy,但我的数据库设置为yy-mm-dd所以我希望它转换为db使用但仍显示dd / mm / yy表示选择字段中的用户。

型号:

public function search($auditCriteria=null)
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;
            $cni = new ContractNoteItem();

            $criteria->with =array('contractNoteItems'=>array('select'=>'{{contractNoteItems}}.*','joinType'=>'LEFT OUTER JOIN', ), );


            if(isset($_GET['cni_client_account_no']) && $_GET['cni_client_account_no']!=0 && $_GET['cni_client_account_no']!=""){
              //  echo 2;
                if(isset($_GET['cni_portfolio_id2']) && $_GET['cni_portfolio_id2']!=0 && $_GET['cni_portfolio_id2']!="")
                {
             //      echo 3;
                    $criteria->join = 'RIGHT JOIN ial_contract_note_item ON ial_contract_note_item.cni_contract_note_id=t.id';
                    $criteria->condition = 'ial_contract_note_item.cni_contract_note_id=t.id';
                    $criteria->compare('cni_contract_note_id',$this->id);
                    $criteria->compare('ial_contract_note_item.cni_portfolio_id',$_GET['cni_portfolio_id2']);
                }else{
               //     echo 4;
                    $criteria->join = 'RIGHT JOIN ial_contract_note_item ON ial_contract_note_item.cni_contract_note_id=t.id';
                    $criteria->condition = 'ial_contract_note_item.cni_contract_note_id=t.id';
                    $criteria->compare('cni_contract_note_id',$this->id);
                    $criteria->compare('ial_contract_note_item.cni_client_account_no',$_GET['cni_client_account_no']);
                }
             //   echo 5;
            }
           // echo 6, 

    $criteria->compare('id',$this->id,true);
    $criteria->compare('cn_fund_house_id',$this->cn_fund_house_id,true);
    $criteria->compare('cn_investment_date',$this->cn_investment_date,true);
    $criteria->compare('cn_settlement_date',$this->cn_settlement_date,true);
            $criteria->compare('transaction_id',$this->transaction_id,true);
            $criteria->compare('sub_transaction_id',$this->sub_transaction_id,true);
            $criteria->compare('counter_party_trans_ref',$this->counter_party_trans_ref,true);
            $criteria->compare('t.modified_by',$this->modified_by,true);
          //  $criteria->compare('ial_contract_note_item.cni_portfolio_id',$this->cni_portfolio_id,true);


            if($auditCriteria != null && $auditCriteria)
            {
                //$criteria->compare('actual',1);
                $criteria->compare('t.pending',1);
            }

            if(!$auditCriteria)
            {               
                $criteria->compare('t.actual',1);
                $criteria->compare('t.pending',0);
            }

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

那我该怎么做呢?我用altField尝试了altFormat,但是它完全覆盖了日期格式,并且数据库无法识别实际的dateFormat.I知道它必须与before和after函数有关,但似乎它们没有被加载

1 个答案:

答案 0 :(得分:0)

您可以在模型类中创建一个setter和getter

public function setDate($value)
{
  $dt=DateTime.createFromFormat('d/m/Y',$value);
  $this->your_date=$dt->format('Y-m-d');
}
public function getDate()
{
  $dt=DateTime.createFromFormat('Y-m-d',$this->your_date);
  return $dt->format('d/m/Y');
}

现在让widget使用模型的属性'Date'(set / get之后的单词)。

根据您的喜好调整变量名称