我有一个奇怪的问题,我有一个模型和一个属性是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;
}
}
是否有人在搜索时使用时间戳字段转换?需要帮助!
答案 0 :(得分:0)
错误发生在list($day, $month, $year) = explode("/", $this -> create_time)
。这是因为list($variables)
期望一个数组至少具有与要分配的变量相同数量的元素。在执行此行之前,您需要使用缺少的字段(日,月或年)填充日期。
您可以使用datepicker作为字段,以确保$create_time
始终包含日,月和年。
可替换地:
$create_time
。