查询中的Yii2格式日期

时间:2017-03-25 21:55:47

标签: yii2

我需要将日期-week_end-从yyyy-mm-dd格式化为M-d-Y。填充下拉列表。 这是控制器代码:

public static function getPayWeeks()
{
    $droptions = PayWeeks::find()->asArray()->all();
    return ArrayHelper::map($droptions, 'id', 'week_end');
}

以下是表单代码:

<?php $form = ActiveForm::begin(['id' => 'payweek-form']); ?>
     <?= $form->field($model, 'id')->dropDownList(PayWeeks::getPayWeeks(), ['prompt' => ' -- Select Week End Date --']); ?> 
            <div class="form-group">
                <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
            </div>
<?php ActiveForm::end(); ?>

由于

1 个答案:

答案 0 :(得分:1)

创建阵列时,您必须重新格式化week_end字段:

public static function getPayWeeks()
{
    $droptions = PayWeeks::find()->asArray()->all();
    return ArrayHelper::map($droptions, 'id', function($model) {
        return date("M-d-Y", strtotime($model['week_end']));
    });
}