计算哪些时区在PHP上具有特定的当前本地时间

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了计算哪些时区在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的建议使用改编脚本来使用四分之一小时.

脚本宝典总结

以上是脚本宝典为你收集整理的计算哪些时区在PHP上具有特定的当前本地时间全部内容,希望文章能够帮你解决计算哪些时区在PHP上具有特定的当前本地时间所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。