计算哪些时区在PHP上具有特定的当前本地时间
发布时间:2020-12-13 22:31:50 所属栏目:PHP教程 来源:网络整理
导读:我的应用程序需要在当地时间下午6:00向所有用户发送电子邮件. 服务器配置为GMT. 我有一个在每小时开始时调用的函数. 如何在下午6:00找出哪个时区( PHP时区)? (这样我就可以查询数据库并查找设置为此时区的所有用户并向他们发送电子邮件). 还可以在不迭代整
我的应用程序需要在当地时间下午6:00向所有用户发送电子邮件.
服务器配置为GMT. 我有一个在每小时开始时调用的函数. 如何在下午6:00找出哪个时区( PHP时区)? (这样我就可以查询数据库并查找设置为此时区的所有用户并向他们发送电子邮件). 还可以在不迭代整个时区阵列的情况下完成吗? 谢谢. 解决方法
我之前的回答并不太好,因为夏令时使您无法在GMT的绝对偏移中定义时区.假设您在
one of these formats中存储时区,这是一个更好的解决方案.此脚本应在每季度每15分钟运行一次.
// Target local time in hours (6:00 PM = 18:00) $targetHour = 18; // Get timestamp of nearest quarter-hour. // (Assuming this is being run by cron on-the-quarter-hour by server time) date_default_timezone_set('GMT'); $currentHourTimestamp = (round(time() / 900) * 900); // Calculate offset in hours to target hour $targetDateTime = new dateTime('@' . $currentHourTimestamp,new DateTimeZone('GMT')); $targetDateTime->setTime($targetHour,0); $targetOffset = $targetDateTime->getTimestamp() - $currentHourTimestamp; // You can get this from the database with a // 'SELECT DISTINCT timezones FROM users' query or similar $timezonesUsed = array('America/Los_Angeles','America/Phoenix','Europe/Budapest','Europe/Dublin'); $matchingTimezones = array(); foreach($timezonesUsed as $localTimezone) { // throws an Exception if timezone is not recognized $localDateTimezone = new DateTimeZone($localTimezone); $localOffset = $localDateTimezone->getOffset($targetDateTime); if($localOffset == $targetOffset) $matchingTimezones[] = $localTimezone; } // Now send emails to users in database with timezones in $matchingTimezones 编辑:根据Catcall的建议使用改编脚本来使用四分之一小时. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |