nedarta/yii2-local-datetime-behavior
Composer 安装命令:
composer require nedarta/yii2-local-datetime-behavior
包简介
Yii2 behavior for converting local datetime input to UTC timestamp and back
README 文档
README
Yii2 behavior for converting local datetime strings (for example from Tempus Dominus) into UTC Unix timestamps for database storage, and converting them back to local time for display in forms and views.
This behavior is designed to eliminate timezone-related bugs (+1h / +2h, DST issues) by enforcing a clear and predictable datetime flow.
Features
- Stores datetime values as UTC Unix timestamps in the database
- Works with local datetime inputs (no timezone attached)
- Handles Daylight Saving Time (DST) correctly
- No usage of
strtotime() - Compatible with:
- Tempus Dominus
- Flatpickr
- HTML5
datetime-local
- Clean, reusable Yii2 behavior
Installation
Install via Composer:
composer require nedarta/yii2-local-datetime-behavior
Usage
Attach the behavior to your ActiveRecord model:
use nedarta\behaviors\LocalDatetimeBehavior; public function behaviors(): array { return [ [ 'class' => LocalDatetimeBehavior::class, 'attribute' => 'datetime', 'format' => 'Y-m-d H:i', 'timezone' => 'Europe/Riga', ], ]; }
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
| attribute | string | datetime | Model attribute storing the timestamp |
| format | string | Y-m-d H:i | Datetime format used in forms |
| timezone | string | Europe/Riga | Local/UI timezone |
Example Flow
User Input (Form)
2025-12-20 20:00
Database Value (UTC)
1766246400
Displayed Output
2025-12-20 20:00
How It Works
- beforeSave
- Converts local datetime string into UTC Unix timestamp
- afterFind
- Converts UTC timestamp back into local datetime string
All timezone logic is centralized in one place.
Best Practices
- Store only UTC timestamps in the database
- Let UI components work in local time
- Never use
strtotime()for user input - Keep timezone conversion out of controllers and views
License
MIT
nedarta/yii2-local-datetime-behavior 适用场景与选型建议
nedarta/yii2-local-datetime-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Behavior」 「timezone」 「datetime」 「utc」 「yii2」 「tempus-dominus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nedarta/yii2-local-datetime-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nedarta/yii2-local-datetime-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nedarta/yii2-local-datetime-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CakePHP plugin to provide easy access to zumba/swivel
Tools to convert between .NET and PHP date formats
Time provider, providing consistent current date, time, datetime and timezone across the request.
This is a set of Yii2 behaviors extensions
Adds the EDTF data type to Wikibase
Datetime helpers for timezone handling
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-19