简体   繁体   English

处理将现有 laravel 应用程序从本地时区转换为 UTC 的最佳方法

[英]Best way to handle converting existing laravel application from local timezone to UTC

Currently, in our Laravel application, we have the timezone set as followed.目前,在我们的 Laravel 应用程序中,我们的时区设置如下。

 'timezone' => env('TIMEZONE', 'Europe/London'),

All our DateTime and timestamps fields in our MySQL database are also stored in the Europe/London timezone. MySQL 数据库中的所有DateTime时间和timestamps字段也存储在Europe/London时区。

Our application is now going to switch over to UTC , however as BST is currently an hour out at the moment if we change the TIMEZONE to UTC in our application all our existing infrastructure will break.我们的应用程序现在将切换到UTC ,但是由于BST目前是一个小时,如果我们在我们的应用程序中将TIMEZONE更改为UTC ,所有现有的基础设施都会中断。

The things that will break include our existing queries using time fields as they will be an hour out of sync with the records in the database.会中断的事情包括我们现有的使用时间字段的查询,因为它们与数据库中的记录不同步一个小时。

An example of where we use time in our system within queries is as followed我们在查询中使用系统中时间的示例如下

Task::where('expired', '=', false)->where('expires_at', '<', Carbon::now())->get();

With the code example above tasks would be expired an hour early because UTC is one hour behind BST .对于上面的代码示例,任务将提前一小时过期,因为UTCBST晚一小时。

As changing to UTC will be a fundamental change for a system I was hoping I could get answers to the following questions.由于更改为UTC将是一个系统的根本变化,我希望我能得到以下问题的答案。

  1. Are there any resources out there that outline ways of converting a system from a local time zone to UTC?是否有任何资源概述了将系统从本地时区转换为 UTC 的方法?
  2. Instead of converting to UTC should I store everything in Europe/London and then convert it for the user on the frontend?我应该将所有内容存储在Europe/London ,然后在前端为用户转换,而不是转换为UTC
  3. Is there any way to easily query a database table that has multiple timezones in it?有什么方法可以轻松查询包含多个时区的数据库表?

Thanks for any help, I know this question is difficult so anything that can point me in the right direction would be greatly appreciated.感谢您的帮助,我知道这个问题很困难,所以任何能指出我正确方向的东西都将不胜感激。

Can't say anything about the first item in your list, and here is what I think about the second.不能说你列表中的第一项,这是我对第二项的看法。 There are actually quite a few timezones you should be aware of: default php timezone (the one from php.ini), server timezone (afaik, if php.ini timezone is empty, it falls back to the one from a server), mysql's timezone, server timezone where mysql instance is deployed, your timezone where you currently are, your users' timezone, and not to mention such exoteric things like mysql connection timezone. There are actually quite a few timezones you should be aware of: default php timezone (the one from php.ini), server timezone (afaik, if php.ini timezone is empty, it falls back to the one from a server), mysql's时区,部署 mysql 实例的服务器时区,您当前所在的时区,您的用户的时区,更不用说诸如 mysql 连接时区之类的公开内容。

With that said, if you don't want to have a pretty fun time debugging fancy error, you want all system timezones to be the same.话虽如此,如果您不想花时间调试花哨的错误,那么您希望所有系统时区都相同。 Actually, it doesn't matter what exactly it is.实际上,它到底是什么并不重要。 More important, you're aware of each of them, and know how they apply to your situation.更重要的是,您了解它们中的每一个,并知道它们如何适用于您的情况。 Having all of your system timezones as UTC is just more predictable, more universal, more common.将所有系统时区设置为 UTC 只是更可预测、更通用、更常见。

But if I were you, I would leave infrastructure timezone settings as they are.但如果我是你,我会保持基础设施时区设置不变。 I doubt you gain anything tangible with this move.我怀疑你会从这一举动中获得任何切实的好处。 Converting datetimes on a frontend is actually a common way of dealing with stuff like that.在前端转换日期时间实际上是处理此类事情的常用方法。 For example, in postgres it's possible to save a timezone along with a datetime, but it's pretty much useless, since it's converted into a system timezone.例如,在 postgres 中,可以将时区与日期时间一起保存,但它几乎没有用,因为它已转换为系统时区。 So when I need to output a datetime to a client, I write something like因此,当我需要 output 给客户一个日期时间时,我会写类似

(new AdjustedAccordingToTimeZone(
    new FromISO8601('2018-04-25 15:08:01+00:00'),
    new Moscow()
))
    ->value();

Regarding your third question, quick googling pointed me at this one .关于你的第三个问题,快速谷歌搜索指出了这个问题 So if you query several rows, each of which has its own timezone, you can always convert those dates to any timezone you wish.因此,如果您查询多行,每行都有自己的时区,您可以随时将这些日期转换为您希望的任何时区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM