我有一個奇怪的問題,我有一個模型和一個屬性是create_time(timestamp)。我必須將時間戳顯示爲用戶友好日期(YMD)格式,但問題是當我搜索日期時只使用日期(23)或僅年份(2013),我正在使用PHP警告而不是其工作當整個字符串輸入時。 我已經做了迄今爲止以下:Yii中用戶友好的日期篩選
//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;
}
}
曾與搜索時間戳字段轉換工作的人?需要幫助 !
你會得到什麼警告? – topher 2013-03-07 11:03:34
PHP偏移2類型錯誤! – TNC 2013-03-07 12:15:47