我有一个奇怪的问题,我有一个模型,一个属性是 create_time(时间戳)。我必须在视图中将时间戳显示为用户友好的日期(YMD)格式,但问题是当我搜索日期说仅使用日期(23)或仅年份(2013)时,我收到 php 警告而不是它的工作当输入整个字符串时。到目前为止,我已经完成了以下工作:
//Controller Action:
$dataProvider = new MyModel('search');
$dataProvider -> unsetAttributes();// clear any default values
if (isset($_GET['MyModel'])) {
$dataProvider -> attributes = $_GET['MyModel'];
}
$this -> render('MyView', array('dataProvider' => $dataProvider, ));
//My View page
$this->widget('zii.widgets.grid.CGridView', array(
'id' =>'list-grid',
'dataProvider'=>$dataProvider->search(),
'filter' =>$dataProvider,
'columns'=>array(
array(
'first_name',
'last_name',
'consult_price',
'create_time'=>array('name'=>'create_time',
'header' =>'Profile Created In',
'value'=>'Components::userFriendlyDate($data->id);',// or before validate and afterFind on Model
'htmlOptions' =>array('style'=>'text-align: center'),
..................
// My Model
$criteria = new CDbCriteria;
if (!empty($this -> create_time)) {
// as i say this explodes a string with y-m-d format so i am getting an error.
list($day, $month, $year) = explode("/", $this -> create_time);
$daystart = mktime(0, 0, 0, (int)$month, (int)$day, (int)$year);
$dayend = mktime(23, 59, 59, (int)$month, (int)$day, (int)$year);
$criteria -> condition = ':s<=create_time AND create_time<=:e';
$criteria -> params = array(':s' => $daystart, ':e' => $dayend);
}
$criteria -> compare('create_time', $this -> convert($this -> create_time));
return new CActiveDataProvider($this, array('criteria' => $criteria, ));
//where convert function goes likes this:
public function convert($date) {
if ($date == NULL)
return $date;
//null shows all records
else {
list($day, $month, $year) = explode("/", $this -> create_time);
$daystart = mktime(0, 0, 0, (int)$month, (int)$day, (int)$year);
$dayend = mktime(23, 59, 59, (int)$month, (int)$day, (int)$year);
$sql = "SELECT create_time FROM MYTable WHERE create_time BETWEEN '$dayend' AND '$daystart'";
$result = Yii::app() -> db -> createCommand($sql) -> queryAll();
return $result;
}
}
有没有人在搜索中使用时间戳字段转换?需要帮助 !