jc-it/yii2-datetime-behavior
Composer 安装命令:
composer require jc-it/yii2-datetime-behavior
包简介
Objects of date/time/timestamp fields via magic getter and setter.
README 文档
README
This extension provides a package to help working with datetime objects in (most often) Active Record objects.
It provides magic getters and setters to work with DateTime objects instead of having to work with strings. It does this with proxies to keep rules (i.e.) working on the standard attributes.
$ composer require jc-it/yii2-datetime-behavior
or add
"jc-it/yii2-datetime-behavior": "^<latest version>"
to the require section of your composer.json file.
Configuring
In a model:
/** * @return array */ public function behaviors(): array { return ArrayHelper::merge( parent::behaviors(), [ DateTimeBehavior::class => [ 'class' => DateTimeBehavior::class ], ] ); }
For an Active Record model it will automatically detect date/time/datetime/timestamp fields and will apply the behavior to them.
The full configuration:
/** * @return array */ public function behaviors(): array { return ArrayHelper::merge( parent::behaviors(), [ DateTimeBehavior::class => [ 'class' => DateTimeBehavior::class, 'attributes' => [ // Map of attribute to type. 'date_attribute' => DateTimeBehavior::TYPE_DATE, 'datetime_attribute' => DateTimeBehavior::TYPE_DATETIME, 'time_attribute' => DateTimeBehavior::TYPE_TIME, ], 'attributeSuffix' => 'Object', // Suffix that will be used to detect if the behavior must be triggered. 'attributeTimezone' => 'UTC', //When storing datetime values in timestamp fields this should default timezone of your database. 'dateTimeClass' => Carbon::class, //The class of the datetime objects to be returned. Must extend from Carbon. ], ] ); }
Usage
If the attributes that are affected by the behavior are accessed with the suffix an object of the configured class will be returned.
// Event has attribute from and until which are timestamps in the database // Assuming in timezone Europe/Amsterdam $event = Event::find()->one(); var_dump($event->from); var_dump($event->fromObject); // Will output string '2019-01-01 12:00:00' (length=19) object(Carbon\Carbon) public 'date' => string '2019-01-01 13:00:00.000000' (length=26) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/Amsterdam' (length=16)
Credits
License
The MIT License (MIT). Please see LICENSE for more information.
jc-it/yii2-datetime-behavior 适用场景与选型建议
jc-it/yii2-datetime-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jc-it/yii2-datetime-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jc-it/yii2-datetime-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-30