PHPackages                             nedarta/yii2-local-datetime-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. nedarta/yii2-local-datetime-behavior

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

nedarta/yii2-local-datetime-behavior
====================================

Yii2 behavior for converting local datetime input to UTC timestamp and back

1.0.0(4mo ago)01MITPHPPHP &gt;=7.4

Since Dec 19Pushed 4mo agoCompare

[ Source](https://github.com/nedarta/yii2-local-datetime-behavior)[ Packagist](https://packagist.org/packages/nedarta/yii2-local-datetime-behavior)[ RSS](/packages/nedarta-yii2-local-datetime-behavior/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

yii2-local-datetime-behavior
============================

[](#yii2-local-datetime-behavior)

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
--------

[](#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
------------

[](#installation)

Install via Composer:

```
composer require nedarta/yii2-local-datetime-behavior
```

---

Usage
-----

[](#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
---------------------

[](#configuration-options)

OptionTypeDefaultDescriptionattributestringdatetimeModel attribute storing the timestampformatstringY-m-d H:iDatetime format used in formstimezonestringEurope/RigaLocal/UI timezone---

Example Flow
------------

[](#example-flow)

### User Input (Form)

[](#user-input-form)

```
2025-12-20 20:00

```

### Database Value (UTC)

[](#database-value-utc)

```
1766246400

```

### Displayed Output

[](#displayed-output)

```
2025-12-20 20:00

```

---

How It Works
------------

[](#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
--------------

[](#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
-------

[](#license)

MIT

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance74

Regular maintenance activity

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

144d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5b419e3f062d93e558b33278c937dacbb5367edf84b8d8b134ba1d86d56f185b?d=identicon)[nedarta](/maintainers/nedarta)

---

Top Contributors

[![nedarta](https://avatars.githubusercontent.com/u/16962105?v=4)](https://github.com/nedarta "nedarta (2 commits)")

---

Tags

datetimeyii2Behaviortimezoneutctempus dominus

### Embed Badge

![Health badge](/badges/nedarta-yii2-local-datetime-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/nedarta-yii2-local-datetime-behavior/health.svg)](https://phpackages.com/packages/nedarta-yii2-local-datetime-behavior)
```

###  Alternatives

[mdmsoft/yii2-autonumber

Auto number extension for the Yii framework

1830.9k](/packages/mdmsoft-yii2-autonumber)[baibaratsky/yii2-serialized-attributes-behavior

Yii2 Serialized Attributes Behavior

1174.1k9](/packages/baibaratsky-yii2-serialized-attributes-behavior)[asinfotrack/yii2-audittrail

Yii2-audittrail is a behavior and a set of widgets to track all modifications performed on a model

1727.0k](/packages/asinfotrack-yii2-audittrail)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
