如何在Yii2中使用日期格式获取月份和年份

如何在Yii2中使用日期格式获取月份和年份,yii2,date-format,Yii2,Date Format,假设我有一张桌子 Date Item Qty 15/1/2016 Item1 10 16/1/2016 Item1 20 16/2/2016 Item1 30 18/2/2016 Item1 10 在报告中,我想这样显示 Month Qty 01-2016 30 02-2016 40 请告诉我如何在Yii2中做到这一点 我已经在我的搜索模型中尝试了以下内容 $query = Productsalesdetails::find()

假设我有一张桌子

Date    Item    Qty
15/1/2016   Item1   10
16/1/2016   Item1   20
16/2/2016   Item1   30
18/2/2016   Item1   10
在报告中,我想这样显示

Month   Qty
01-2016 30
02-2016 40
请告诉我如何在Yii2中做到这一点

我已经在我的搜索模型中尝试了以下内容

$query = Productsalesdetails::find()
        ->select(['DATE_FORMAT(date, "%m-%Y"),productname,sum(total) as total'])
        ->groupBy('DATE_FORMAT(date, "%m-%Y")');

对于查询,也使用字母select和别名作为日期\格式

$query = Productsalesdetails::find()
    ->select( 'DATE_FORMAT(date, "%m-%Y") as m_date, productname, sum(total) as total')
    ->groupBy ('m_date, productname');
您是否已经在“选择”中使用了格式?您是否可以在“属性”中使用别名

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'summaryOptions' => ['class' =>'dfenx_pagination_summary',],
    'pager' => ['options' => ['class'=> 'pagination pull-right']],
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
           'attribute' => '', 
           'label' => 'Month',  
           'value' => function ($model) {       
                return $model->m_date;
           },        
        ],
        [
           'attribute' => 'productname', 
           'label' => 'Item',  
        ],
         [
           'attribute' => '', 
           'label' => 'Total',  
           'value' => function ($model) {       
                return $model->total;
           },        
        ],

       ....

    ]); 

但我需要按月份分组。你可以看到我已经按月份对它进行了分组。很抱歉,问题的标题似乎是关于格式的。。或者是两个问题合一。。。最终发布代码以选择相关数据。。你有模特吗。。数据提供者。。使用栅格视图..是。我的问题不是100%正确。我想要分组方式和日期格式。我已在问题中添加了我的搜索模型。获取未知属性:backend\modules\…\models\Productsalesdetails::m\u date我已更新答案,避免了属性中的m\u date名称。请先尝试使用thgis。。。但是你可能需要在你的模型中使用合适的公共变量来支持查询结果,我已经更新了答案。。
class YourModel extends \yii\db\ActiveRecord
{
   public $m_date;
   public $total;
   .....