PHPackages                             zssarkany/laravel-database-sticky-timezone - 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. [Database &amp; ORM](/categories/database)
4. /
5. zssarkany/laravel-database-sticky-timezone

ActiveLibrary[Database &amp; ORM](/categories/database)

zssarkany/laravel-database-sticky-timezone
==========================================

Service for keeping database connection timezone synchronized to PHP default timezone

v1.0.4(4y ago)23041MITPHPPHP &gt;=7.1

Since Jun 3Pushed 4y ago1 watchersCompare

[ Source](https://github.com/zssarkany/laravel-database-sticky-timezone)[ Packagist](https://packagist.org/packages/zssarkany/laravel-database-sticky-timezone)[ Docs](https://github.com/zssarkany/laravel-database-sticky-timezone)[ RSS](/packages/zssarkany-laravel-database-sticky-timezone/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (7)Used By (0)

laravel-database-sticky-timezone
================================

[](#laravel-database-sticky-timezone)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8b2b086daf2300b3edcb162040ea0dedd4a2f845c3887673ac55a0790c15a9b1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a737361726b616e792f6c61726176656c2d64617461626173652d737469636b792d74696d657a6f6e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/zssarkany/laravel-database-sticky-timezone)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/bd4458b5ba6bdbae93187a1ec38672200e6127310ba36ca4dcce735cfe5ce65e/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f7a737361726b616e792f6c61726176656c2d64617461626173652d737469636b792d74696d657a6f6e652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/zssarkany/laravel-database-sticky-timezone)[![StyleCI](https://camo.githubusercontent.com/3c42b59d7e1ae5cdbfafb608bd1de576c976c611ee6b4edb1812f5d289662a55/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3139303030353732372f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/190005727)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/daf173a91fb5bd8a5269df310dd58172d657d23f06d8366ce0a39f336e012d76/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7a737361726b616e792f6c61726176656c2d64617461626173652d737469636b792d74696d657a6f6e652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/zssarkany/laravel-database-sticky-timezone/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/3432575eb5b7c0e75c9846202ef2ee1b7fc60511cb0c483ce61301ad21186c6e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7a737361726b616e792f6c61726176656c2d64617461626173652d737469636b792d74696d657a6f6e652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/zssarkany/laravel-database-sticky-timezone/?branch=master)[![Open Issues](https://camo.githubusercontent.com/b8196012555ee2ae8112c5ed52b720ecd5526d7bd1cfdd85eb41f9d2d47ede5d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d7261772f7a737361726b616e792f6c61726176656c2d64617461626173652d737469636b792d74696d657a6f6e652e7376673f7374796c653d666c61742d737175617265)](https://github.com/zssarkany/laravel-database-sticky-timezone/issues)[![Total Downloads](https://camo.githubusercontent.com/14fb441e51fd76b117e65ffcf0b9ed752d47fba53afcde6f653b7d394b439612/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7a737361726b616e792f6c61726176656c2d64617461626173652d737469636b792d74696d657a6f6e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/zssarkany/laravel-database-sticky-timezone)

Service for keeping database connection timezone synchronized to PHP default timezone.

Purpose
-------

[](#purpose)

There are at least two different approaches for handling database fields of `timestamp` type in projects:

- fetch data using some predefined timezone offset and adjust resulted string representation values with JavaScript or PHP;
- adjust database connection by setting its timezone offset, then fetch timestamp values with desired string representation.

Obviously there are pros and cons for both approaches, but if there is a requirement to support LIKE matching (e.g.: 2019-06-% or %19-06%), the first approach sems to be quite challenging.

This package aims to implement the second approach.

Storage engines store these values as 32bits unsigned integers, and string representation (YYYY-MM-DD HH:MM:SS) depends on the connection-specific timezone offset. Select, update, where expressions could provide unexpected results without appropriate caution.

Under the hood `Illuminate\Database\MySqlConnection` gets extended and `getPdo()`and `getReadPdo()` methods get overwritten by a change detector and actuator logic. When mentioned methods are invoked, underlying PDO object gets checked for replacement and PHP default timezone offset (`date('P')`) gets compared to previously set offset value. In case of change is detected, `'SET time_zone = "+hh:mm"'`is executed on the specific PDO instance, where +hh:mm equals to `date('P')`.

This makes developer able to simply update PHP default time zone with `date_default_timezone_set(...)` and database queries executed afterwards will be affected by the new offset. Effect is limited for `timestamp` fields, `datetime` is not affected.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1
- Laravel &gt;= 5.5

See [PHP and Laravel unit and integration tests](#php-and-laravel-unit-and-integration-tests)

Install
-------

[](#install)

Via Composer

```
$ composer require zssarkany/laravel-database-sticky-timezone
```

It is not necessary to add service provider to `config/app.php`, but if service discovery is disabled via `composer.json`, `ZsSarkany\LaravelDatabaseStickyTimezone\DatabaseStickyTimezoneServiceProvider::class`needs to be added before `Illuminate\Database\DatabaseServiceProvider::class` to `providers` array, otherwise the the connection resolver will be bound after the database connection is resolved already.

Usage
-----

[](#usage)

Append `sticky_timezone => true` to mysql connection configuration in `config/database.php` to enable automatic timezone adjustment.

Example:

```
return [
    // ...
    'connections' => [
        // ...
        'mysql' => [
            // ...
            'sticky_timezone' => true,
        ],
        // ...
    ],
    // ...
];
```

SQL statements executed on this connection are taking PHP default timezone into account for timestamp fields.

PHP and Laravel unit and integration tests
------------------------------------------

[](#php-and-laravel-unit-and-integration-tests)

Package is tested with the following conditions:

PHPLaravelStability7.15.5stable7.15.6stable7.15.7stable7.15.8stable7.25.5stable7.25.6stable7.25.7stable7.25.8stable7.26.0stable7.26.2stable7.35.5stable7.35.6stable7.35.7stable7.35.8stable7.36.0stable7.36.2stableSupported drivers
-----------------

[](#supported-drivers)

Package currently support MySQL driver only, but it should be easy to implement support for other drivers, which are provided by Laravel.

Contributions are welcome :)

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Zsolt Sarkany](https://github.com/zssarkany)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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

Every ~239 days

Total

5

Last Release

1579d ago

PHP version history (2 changes)v1.0.0PHP ~7.1

v1.0.4PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/f2e3e8f8fae0463e5b3f8b68ebf650ca222f9f2c6fafce4bcd27519a75c8d42b?d=identicon)[zssarkany](/maintainers/zssarkany)

---

Top Contributors

[![zssarkany](https://avatars.githubusercontent.com/u/7733472?v=4)](https://github.com/zssarkany "zssarkany (15 commits)")

---

Tags

zssarkanylaravel-database-sticky-timezone

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/zssarkany-laravel-database-sticky-timezone/health.svg)

```
[![Health](https://phpackages.com/badges/zssarkany-laravel-database-sticky-timezone/health.svg)](https://phpackages.com/packages/zssarkany-laravel-database-sticky-timezone)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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