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(6mo ago)01MITPHPPHP &gt;=7.4

Since Dec 19Pushed 6mo 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 today

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

28

—

LowBetter than 52% of packages

Maintenance66

Regular maintenance activity

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity35

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

198d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16962105?v=4)[Nedarta](/maintainers/nedarta)[@nedarta](https://github.com/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

[baibaratsky/yii2-serialized-attributes-behavior

Yii2 Serialized Attributes Behavior

1275.5k9](/packages/baibaratsky-yii2-serialized-attributes-behavior)[mdmsoft/yii2-autonumber

Auto number extension for the Yii framework

1831.3k](/packages/mdmsoft-yii2-autonumber)[valentinek/yii2-closure-table-behavior

This extension allows you to get functional for closure table trees.

1615.5k](/packages/valentinek-yii2-closure-table-behavior)[v0lume/yii2-meta-tags

DB based model meta data for SEO

204.1k](/packages/v0lume-yii2-meta-tags)

PHPackages © 2026

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