fin callorca / datetimebundle
通过简化的服务器-数据库-客户端时区管理以及额外的 DateTime 方法扩展 symfony 项目。
此包的规范存储库似乎已丢失,因此该包已被冻结。
Requires
- php: >=7.3.0
- doctrine/dbal: ^2.5
- prokki/ext-datetime: ^0.0.0
- symfony/config: ^4.3
- symfony/dependency-injection: ^4.3
- symfony/http-foundation: ^4.3
- symfony/http-kernel: ^4.3
- symfony/yaml: ^4.3
Requires (Dev)
- phpunit/phpunit: ^8.2
- symfony/phpunit-bridge: ^4.3
- symfony/test-pack: ^1.0
README
symfony DateTimeBundle 提供了一些功能,以解决当前 php、symfony 和 doctrine 日期管理支持的不足。
-
使用新的 DateTime/DateTimeImmutable 方法。
-
通过方法访问 Symfony 的 http 请求时间,而不是通过全局
$_SERVER
属性。请参阅类 Request。 -
始终在数据库中保存 datetime 值到相同的时区。
-
轻松切换到不同的预配置时区设置(可用配置:服务器、数据库和客户端)。
-
使用 doctrine 的列类型日期作为主键。
-
为 \DateTimeZone 对象缓存提供一个新中心点。
所有这些功能都是可选的。您可以在不使用任何这些功能或仅使用一个或几个功能的情况下安装此包。
目录
需求
PHP v7.3 的使用是强制性的。强烈建议从 Symfony v4 开始。
如果您想在使用 Symfony v3(和 PHP _v5.*)的库中,请使用版本 0.* 的发布版本。
集成
composer require fincallorca/datetimebundle "^1.0"
用法
要使用新的 datetime 方法,请将现有 \DateTime
/\DateTimeImmutable
对象的实例化替换为提供的 \Fincallorca\DateTimeBundle\Component\DateTime/\Fincallorca\DateTimeBundle\Component\DateTimeImmutable 类。
这些类提供了 一些额外的有用 datetime 方法,如
$dateTime->duplicate()
$dateTime->addHours()
$dateTime->toEndOfDay()
- 等。
请访问 prokki/ext-datetime 获取有关所有新功能的更多信息。
此外,datetime 类支持所有预配置的时区。使用以下方法
$dateTime->toServerDateTime()
$dateTime->toDatabaseDateTime()
$dateTime->toClientDateTime()
来 快速切换时区。有关如何配置时区的说明,请参阅章节 配置时区。
在类 Request 中启用支持
当前的 Symfony 请求类 无法正确返回请求的时间值。特别是如果需要频繁访问请求的时间,更改请求对象将帮助您节省大量时间。
要在您的控制器中使用 \Fincallorca\DateTimeBundle\Component\HttpFoundation\Request 类,请将您的 public/index.php
或所有 public/app*.php
文件中的当前请求类替换为新类(Symfony v3.*)。
// comment follwing line //use Symfony\Component\HttpFoundation\Request; // and add the new request class use Fincallorca\DateTimeBundle\Component\HttpFoundation\Request;
此外,在所有控制器中或至少在这些控制器中执行相同的操作,以便访问请求的时间。
之后,您可以访问在RequestDateTimeInterface中指定的请求时间方法。
示例
use Fincallorca\DateTimeBundle\Component\HttpFoundation\Request; class MyController extends Controller /** * @param Request $request * * @return Response */ public function indexAction(Request $request) { // get the request's date time $requestDateTime = $request->getDateTime(); }
配置时区
可能性
-
将所有日期时间保存在一个共同的时间区域中,
-
以及初始化一个通用的客户端时间区域
都是可选的。
要配置其中之一,您需要创建一个新的文件 config/packages/datetime.yml
并插入以下内容
datetime: database: ~ client: ~
配置数据库时区
仅将所有日期时间对象保存在一个共同的时间区域中(即使用 UTC),修改配置变量 database
datetime: # feel free to change the timezone name to your needs! database: "UTC" client: ~
⚠️ 但请注意
- 从数据库读取的所有日期时间都使用新的数据库时间区域初始化,
- 将所有保存到数据库中的日期时间在保存之前转换为给定的时间区域。
配置默认客户端时区
您可以通过简单地更改配置中的变量 client
来设置客户端的默认时间区域
datetime: database: ~ # both variables (database and client) are optional... # if you do not specify the timezones, all times will be saved with your default server time client: "Europe/London"
与配置的数据库时间区域不同,您可以在运行时通过调用Fincallorca\DateTimeBundle\Component\DateTimeKernel::setTimeZoneClient()来更改默认客户端的时间区域
使用时区缓存
如果您与时间区域工作得非常频繁,您可能希望避免多次实例化时间区域对象。因此,Fincallorca\DateTimeBundle\Component\DateTimeKernel 类提供了一个静态方法 getTimeZoneByName()
来处理缓存的时间区域。
多次调用 DateTimeKernel::getTimeZoneByName()
并传递相同的时间区域名称将始终返回相同的时间区域对象。
要确保使用时间区域缓存,将所有 \DateTimeZone 对象的实例化从 new \DateTimeZone($STRING)
更改为 DateTimeKernel::getTimeZoneByName($STRING)
。
已弃用:Symfony v3
通过 composer 安装包
composer require fincallorca/datetimebundle "^0.0"
通过扩展您的 AppKernel.php
来添加新包
class Kernel extends \Symfony\Component\HttpKernel\Kernel { public function registerBundles() { return [ // [...] new Fincallorca\DateTimeBundle\DateTimeBundle(), ]; } // [...] }
通过将时间区域配置添加到您的 config.yml
来配置包
datetime: database: "UTC" client: "Europe/London"
致谢
非常感谢prokki/ext-datetime 扩展了 \DateTime
/\DateTimeImmutable
类。