用 PHP 处理国际时间
关于使用国际时区和国际时区的一个简单问题格林威治标准时间。
我有一个应用程序,需要根据他们的时间/区 GMT +- 在特定时间(早上 6 点)向国际上的每个人发送一封电子邮件。我不知道如何用 GMT 和 GMT 来处理这个问题。时区。任何意见都会受到赞赏。
Just a quick question on working with International Timezones & GMT.
I have an app that needs to send out an email at a certain time (6am) for every person internationally based on their time/zone GMT +-. I'm at a loss on how this should be handled with GMT & Timezones. Any input is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
天真地,您可以收集(或以某种方式确定)时区 ID (tzid) 为每个用户,只需在每个 UTC 小时的顶部运行一个 cron 作业,检查当前时间是否为06:00 在任何这些区域。
但这并不容易,因为有几个时区与 UTC 没有整小时偏移,例如 America/St_Johns (UTC-03:30) 和 Asia/Kathmandu (UTC+05:45),并且如果您仅每小时运行一次 cron,这些用户将不会收到任何电子邮件。因此,为了安全起见,您可能希望每刻钟运行一次 cron。
但由于 cron 作业通常会在计划运行后几秒启动,因此您不应该测试与 06:00:00 的相等性。此外,时区不必以刻钟为增量(但值得庆幸的是,所有公认的时区都是如此),因此迂腐一点,您可能需要评估当前时间是否在某个窗口。
例如,如果您每 15 分钟运行一次 cron,请将窗口设置为 15 分钟宽(例如 05:55 到 06:10),以确保捕获边缘情况。理论上仍然有可能跳过 cron 运行...但是如果不存储指示某人是否已发送今天的电子邮件的额外数据(如果没有则弥补差额),这就是最好的选择可以做。
Naïvely, you could collect (or somehow determine) a time zone ID (tzid) for each of your users, and simply run a cron job at the top of every UTC hour checking if the current time is 06:00 in any of those zones.
It's not that easy, though, because there are several time zones that don't have whole-hour offsets from UTC, such as America/St_Johns (UTC-03:30) and Asia/Kathmandu (UTC+05:45), and these users wouldn't get any emails if you only ran the cron hourly. So you might want to run the cron every quarter-hour just to be on the safe side.
But since cron jobs often start a few seconds after they're scheduled to run, you shouldn't be testing for equality with 06:00:00. Furthermore, time zones don't have to be in quarter-hour increments (but thankfully, all commonly recognized zones are), so to be pedantic, you might want to evaluate whether the current time falls within a certain window.
If, for example, you're running your cron every 15 minutes, make that window 15 minutes wide (e.g., 05:55 to 06:10) to be sure to catch the edge cases. There's still the theoretical chance that a cron run could be skipped... but without storing additional data that indicates whether or not someone has yet been sent today's email (and making up the difference if they haven't), that's about the best you can do.
您可以使用 php 时区设置函数,如下所示
date_default_timezone_set('America/Los_Angeles');
通过使用此功能,您可以获得用户的时间。在此基础上您可以发送电子邮件。
更多参考date_default_timezone_set
You can use the php timezone set function as like below
date_default_timezone_set('America/Los_Angeles');
With the use of this function you can get the time of the user. Based on this you can send the email.
For more referecnce date_default_timezone_set
您可以运行一个 cron 来每小时检查一下您的用户是否存在于凌晨 6 点的某个地方。
You could run a cron to check every hour if one of your user exists somewhere where its 6am.