yii2使用时间插件

最近在学习yii2 框架,发现框架很强大,而且结合了时下许多新的技术在里面。
简单记录一下yii2-date-picker插件的使用方法:
首先先了解一下github中的相关资源:https://github.com/2amigos/yii2-date-picker-widget
其次:在自己的项目中利用上面链接中的相关方法下载此资源,命令为:
php composer.phar require “2amigos/yii2-date-picker-widget” “*”
最后,在视图文件中使用下面的代码:
use dosamigos\datepicker\DatePicker;
例子中提供了两种方法:
1> 使用widget方式

<?= DatePicker::widget([
    'model' => $model,
    'attribute' => 'date',
    'template' => '{addon}{input}',
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'dd-M-yyyy'
        ]
]);?>
2> ActiveForm方式
<?= $form->field($model, 'date')->widget(
    DatePicker::className(), [
        // inline too, not bad
        'inline' => true, 
        // modify template for custom rendering
        'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
        'clientOptions' => [
            'autoclose' => true,
            'format' => 'dd-M-yyyy'
        ],
        'options'=>[
            $model->date = date('Y-m-d H:i:s'),//格式化初始时间
        ]
]);?>

在model文件里面找到对应的字段,更改rule方法,格式化时间成为时间戳

  ['paytime' ,  'filter', 'filter' => function(){
            return strtotime($this->paytime);
  }],
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Yii2框架是一个基于PHP的Web应用程序开发框架,而PhpSpreadsheet是一个用于操作Excel文件的PHP类库。结合使用Yii2和PhpSpreadsheet插件可以实现Excel文件的导入和导出功能。 以下是使用PhpSpreadsheet插件实现Excel文件导入和导出的示例代码: 1. Excel文件导入 在控制器中引入PhpSpreadsheet插件: ```php use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; ``` 在action中实现Excel文件导入: ```php public function actionImportExcel() { $inputFileName = 'path/to/file.xlsx'; // 文件路径 $spreadsheet = IOFactory::load($inputFileName); $worksheet = $spreadsheet->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); for ($row = 1; $row <= $highestRow; ++$row) { for ($col = 0; $col < $highestColumnIndex; ++$col) { $cellValue = $worksheet->getCellByColumnAndRow($col, $row)->getValue(); // 处理单元格数据 } } } ``` 2. Excel文件导出 在控制器中引入PhpSpreadsheet插件: ```php use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; ``` 在action中实现Excel文件导出: ```php public function actionExportExcel() { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World!'); $writer = new Xlsx($spreadsheet); $filename = 'export.xlsx'; // 文件名称 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); $writer->save('php://output'); } ``` 以上是使用Yii2框架和PhpSpreadsheet插件实现Excel文件导入和导出的示例代码,可以根据实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值