timostamm/doctrine-fixed-timezone
Composer 安装命令:
composer require timostamm/doctrine-fixed-timezone
包简介
Normalize timezones in a Doctrine database.
README 文档
README
The problem
If your application handles dates with different timezones, you have two options:
1) save the time zone of each date to the database
If you need to know the timezone later on, this is the only option.
Unfortunately, this makes queries on dates practically impossible:
SELECT * FROM x WHERE x.date > CURRENT_TIMESTAMP() will lead to
unexpected results. CURRENT_TIMESTAMP will use the current time of
the database server, and your dates are interpreted in the wrong
timezone.
2) Convert all dates saved in the database to one timezone
If you convert all dates to the timezone of the database server, your queries will work as expected.
Unfortunately, Doctrine does not come with an option to do this conversion.
Solution provided by this library
This library provides replacements for the DBAL types datetime,
and datetime_immutable, which will automatically convert dates
into the database timezone.
When reading a datetime or datetime_immutable, the timezone
will not be converted to the PHP timezone. The dates will refer
to the correct point in time, but have the database timezone.
Regarding standalone date and time
A date without a time, or a time without a date both do not refer
to an absolute point in time. Since PHP does not have types to
represent stand-alone times or dates, developers use DateTime
objects to represent those too, but their timezone should be ignored.
Therefore, it should be fine to use the standard date and time
doctrine types.
How to use
Symfony:
doctrine: dbal: # ... types: datetime: TS\DoctrineExtensions\DBAL\FixedDbTimezone\DateTimeType datetime_immutable: TS\DoctrineExtensions\DBAL\FixedDbTimezone\DateTimeImmutableType
To set the database timezone, define a constant
define('DATABASE_TIMEZONE', 'Europe/Berline'); or
set an environment variable DATABASE_TIMEZONE.
Otherwise, the PHP timezone is used.
Background
timostamm/doctrine-fixed-timezone 适用场景与选型建议
timostamm/doctrine-fixed-timezone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.21k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 timostamm/doctrine-fixed-timezone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 timostamm/doctrine-fixed-timezone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-03