使用 UNIX 时间戳处理 PHP 中的时区偏移

发布于 2024-10-05 19:01:21 字数 458 浏览 0 评论 0原文

我有一张“通知”表,可能会也可能不会在任何 24 小时内发送。每个通知都有自己相对于 GMT 的时区偏移量,因此我有一个偏移量列,该偏移量将以秒为单位+/-。

我需要创建一个 PHP 脚本,该脚本将定期(通过 cron) 检查数据库中是否有已发送的通知,然后如果当天的时间已超过 00:00,则将其重置为未发送。我真的很难找到最好的方法来做到这一点 - 有一个简单的计算我可以做吗?

我正在考虑使用 mktime() [我的服务器采用 GMT] 获取服务器午夜的 UNIX 时间,并以秒为单位计算出偏移量,但我想到的每一种方法似乎都是错误的。

比如:

if(!mktime()-$notifcationTimezone > $serverMidnight){}

我并不是说这不起作用,但我认为当服务器过去一天时它不会起作用。这让我很困惑,我对此没有信心。

I have a table of 'notifications' which may or may not be sent in any 24 hour period. Each notification has it's own timezone offset from GMT, so I have a column for offset which will be +/- in seconds.

I need to create a PHP script which will regularly (via cron) check the database for sent notifications, and then reset them to unsent if their time has passed 00:00 for that day. I'm really struggling with the best way to do this - is there a simple calculation I can make?

I was thinking of something around getting the UNIX time for the server's midnight using mktime() [my server is in GMT], and working it out with the offset in seconds, but every way I think of seems wrong.

Something like:

if(!mktime()-$notifcationTimezone > $serverMidnight){}

I'm not saying this doesn't work, but I don't think it will work when the server elapses into another day. It just confuses me, and I have no faith in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-10-12 19:01:21

这不是只是与 strtotime 进行简单比较吗,甚至不需要添加偏移量。您可以使用 date 函数设置要传递给要用于比较的 strtotime 的特定时间。

为了让我们更好地了解,请向我们展示数据库中的真实示例以及 cron 现在的样子。

我不完全确定你想在这里做什么,但是,你可能想考虑的是你真的想重置任何东西吗?是否需要?您可以填写发送通知的日期。如果它与今天的日期不匹配,您想再次重新发送吗?

Isnt this just a simple comparison with strtotime and you dont even need to add up the offset. You can use the date function to set a specific time you want to pass to the strtotime which you want to use to compaire against.

To give us a better idea show us a real example from you database and what the cron looks like right now.

I'm not totaly sure what you are trying to pull of here but, what you might wanna think about is do you really want to reset anything, is it needed? You could fill in at what date your notification got send. If it does not match the date of today you want to resend it again?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文