d3yii2/yii2-datetime
Composer 安装命令:
composer require d3yii2/yii2-datetime
包简介
Yii2 extension that adds helpers and behaviors for working with date and time
README 文档
README
This extension helps you to deal with date and time attributes of the models for Yii framework 2.0
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist omnilight/yii2-datetime "*"
or add
"omnilight/yii2-datetime": "*"
to the require section of your composer.json.
Why?
When working with models and forms it is often needed to provide user a way to edit attributes that holds date and/or time. In this case typical problems are:
- Date/time formats could be different in the database and in the displayed form (due to local settings, for ex.)
- You should validate values entered by the user
So you have to set correct format for database, form, somehow convert them from one to another, and it would be nice if you can setup correct formats once (on application level) and lately do not worry about them. This extension helps is this area.
The idea
Idea is to have separate attribute for each editable date/time property of the model, that will be used in the form. This attribute will not be stored in the DB, but it will be used to present user with correctly formatted value. And when you assign value to this attribute, it will be automatically converted and assigned to the DB property of the model.
This extension provides special behavior, that automates work with this attributes
How to use
In your model:
/** * @property string posted_at This is your property that you have in the database table, it has DATETIME format */ class Post extends ActiveRecord { // ... Some code here public function behaviors() { return [ 'datetime' => [ 'class' => DateTimeBehavior::className(), // Our behavior 'attributes' => [ 'posted_at', // List all editable date/time attributes ], ] ]; } }
Now in your view with the form:
// $model has instance of Post <?= $form->field($model, 'posted_at_local')->widget(\yii\jui\DatePicker::className(), \omnilight\datetime\DatePickerConfig::get($model, 'posted_at_local')) ?> // DatePickerConfig is used to properly configure widget. Currently it only supports DatePicker from the Jui extension
That's all! User will enter date in the his local format and will be converted to the database automatically.
How is works
Behavior creates "virtual" attribute named attribute_name_local for each attribute you define in the 'attributes' section.
When you read $yourModel->attribute_name_local behavior will return object with the type DateTimeAttribute. If
this object will be used in the string context, it will be converted to string with the magical __toString method.
And during this original value of attribute_name will be converted into the local representation.
When you assign value to the $yourModel->attribute_name_local internally it will be assigned to value property
of the DateTimeAttribute class and converted to the your database-stored property.
You can also define individual configuration for each attribute and define it's local name, format and so on.
d3yii2/yii2-datetime 适用场景与选型建议
d3yii2/yii2-datetime 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 273 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「date」 「datetime」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 d3yii2/yii2-datetime 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 d3yii2/yii2-datetime 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 d3yii2/yii2-datetime 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Time provider, providing consistent current date, time, datetime and timezone across the request.
Adds the EDTF data type to Wikibase
Datetime helpers for timezone handling
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
A powerful event calendar Tool for Laravel's Nova 5.
统计信息
- 总下载量: 273
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-04-19