【问题标题】:Yii jui date picker formatYii jui 日期选择器格式
【发布时间】:2014-11-12 19:04:31
【问题描述】:

我在 yii\jui\DatePicker 的 _form 文件中使用以下代码,在选择日期时,它正确显示了日期格式。在gridview中渲染的数据是OK的。

但是当我打开记录更新时,显示的格式是yyyy-mm-dd,我想显示的和dd-mm-yyyy一样

<?= $form->field($model, 'birth_date')->widget(DatePicker::className(),


                    [
                        'language' => 'en',
                        'clientOptions' =>[
                        'dateFormat' => 'd-m-yy',
                        'language' => 'US',
                        'country' => 'IN',
                        'showAnim'=>'fold',
                        'yearRange' => 'c-25:c+0',
                        'changeMonth'=> true,
                        'changeYear'=> true,
                        'autoSize'=>true,
                        'showOn'=> "button",
                         //'buttonImage'=> "images/calendar.gif",
                        'htmlOptions'=>[
                        'style'=>'width:80px;',
                        'font-weight'=>'x-small',
                        ],]]) ?> 

谢谢。

【问题讨论】:

    标签: yii2


    【解决方案1】:

    您可以在控制器中格式化日期,然后渲染视图。在控制器中:

    $model->birth_date=Yii::$app->formatter->asDate($model->birth_date, "dd-mm-yyyy");
    

    看看 Yii2 的官方formatter 指南here

    【讨论】:

    • 完美运行。我只是在控制器中添加了修改dd-M-yyyy 的代码。非常感谢。
    • HI @Ali:是否可以在此插件中显示“时间”选项。?因为,我使用的是“Kartik DateTime Picker”。它工作正常。但是,客户希望在日期选择器中使用时间。有可能吗?
    【解决方案2】:

    您可以概括日期格式

    class Setup {
        const DATE_FORMAT = 'php:Y-m-d';
        const DATETIME_FORMAT = 'php:Y-m-d H:i:s';
        const TIME_FORMAT = 'php:H:i:s';
    
        public static function convert($dateStr, $type='date', $format = null) {
            if ($type === 'datetime') {
                  $fmt = ($format == null) ? self::DATETIME_FORMAT : $format;
            }
            elseif ($type === 'time') {
                  $fmt = ($format == null) ? self::TIME_FORMAT : $format;
            }
            else {
                  $fmt = ($format == null) ? self::DATE_FORMAT : $format;
            }
            return \Yii::$app->formatter->asDate($dateStr, $fmt);
        }
    }
    

    然后您可以在其他任何地方(如控制器/模型)访问此函数来转换任何输入的日期/时间字符串以保存到数据库。

    $model->birth_date = Setup::convert($model->dateAttr);
    $model->birth_date = Setup::convert($model->datetimeAttr, 'datetime');
    

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2012-08-03
      相关资源
      最近更新 更多