yii 日期插件
——controller
public $defaultAction = "income";
public function actionIncome(){
$model = new StatsSpace;
$model->user_id = Yii::app()->user->id;
//日期
$idate = isset($_POST['Income']['idate']) ? $_POST['Income']['idate'] : "";
if(!$idate){
$endDate = date("Y-m-d");
$idate = date('m/d/Y',time()-24*3600*5)." - ".date("m/d/Y");
$_POST['Income']['idate']=$idate;
//$idate = date('m/d/Y',strtotime('2012-08-08'))." - ".date("m/d/Y");
}
$model->idate = $idate;
//计算month_income用
$model->idate_from_controller = $idate;
//tabs
$sumall = $model->getSumAllIncome();
//$dataProvider 数据提供者
$dataProvider = $model->getDataProviderIncome($idate);
$this->render("income", array(
'model'=>$model, //$model = new StatsSpace;
'sumall'=>$sumall, //tabs 根据广告位置,算总收入
'idate'=>$idate, //$model->idate_from_controller $model->idate 日期
'dataProvider'=>$dataProvider, //getDataProviderIncome($idate)【下面表格的具体数值】;
));
}
--------------model【data——where】
/**
* tabs相关
*/
public function getSumAll(){
$idate = $this->idate;
if($idate){
$tmp = explode(' - ', $idate);
$startDate = strtotime($tmp[0]);
$d1 = date('Y-m-d', $startDate);
$endDate = strtotime($tmp[1]);
$d2 = date('Y-m-d', $endDate);
}else{
$startDate = strtotime(date("Y-m-d",time()-24*3600*5));
$endDate = strtotime(date("Y-m-d"));
$d1 = date('Y-m-d', $startDate);
$d2 = date('Y-m-d', $endDate);
}
$sql = "SELECT
SUM(totalIncome) AS totalIncomeSum,
SUM(cpcIncome) AS cpcIncomeSum,
SUM(cpmIncome) AS cpmIncomeSum,
monthIncome AS monthIncomeSum
FROM
{{income}}
WHERE
user_id=:user_id
AND
idate>='$d1'
AND
idate<='$d2'";
$uid = Yii::app()->user->id;
$cmd = Yii::app()->db->createCommand($sql);
$row = $cmd->bindValue(':user_id', $uid)->queryRow();
return $row;
}
static public function computeAllData($d1="",$d2=""){
$user_id = Yii::app()->user->id;
if($d1 && $d2){
$where = "WHERE idate >= '{$d1}' AND idate <='{$d2}' AND user_id={$user_id}";
}else{
$where = "WHERE user_id={$user_id}";
}
$sql = "SELECT id,SUM(cpcAllShows) cpcAllShows,SUM(cpcValidClicks) cpcValidClicks,SUM(cpmAllShows) cpmAllShows,SUM(cpmValidShows) cpmValidShows,SUM(monthAllShows) monthAllShows,
SUM(totalIncome) totalIncome,SUM(cpcIncome) cpcIncome,SUM(cpmIncome) cpmIncome,SUM(monthIncome) monthIncome FROM {{income}}
$where
";
$row = Yii::app()->db->createCommand($sql)->queryRow();
return $row;
}
____view
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'htmlOptions'=>array('style'=>'float:left;margin-bottom:-5px;')
)); ?>
<!--日期-->
<div style="float:left; margin-bottom: 20px;">
<div style="float:left;margin-left:5px;height:30px;text-align: bottom;position:position:relative "><p style='position:relative;margin:5px;font-size: 20px'>日期</p></div>
<?php
$this->widget('bootstrap.widgets.TbDateRangePicker', array(
'name'=>'Income[idate]',
'value'=>$model->idate,
'options'=>array(
'showAnim'=>'fold',
),
'htmlOptions'=>array(
'style'=>'height:20px;float:left;'
),
));?>
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'null', 'label'=>'查询','htmlOptions'=>array('style'=>'margin:0 0 10px 5px;')));?>
</div>
<?php $this->endWidget(); ?>