nedarta/yii2-local-datetime-behavior 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nedarta/yii2-local-datetime-behavior

最新稳定版本:1.0.0

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

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固