生成适用于不同时区和DST的计划 [英] Generating a schedule that works over different timezones and DSTs

查看:76
本文介绍了生成适用于不同时区和DST的计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Web应用程序,指导人们提早起床,它为用户提供了七十天的起床时间表。他们输入当前的上升时间和目标上升时间。然后,上升时间会每周减少固定量,直到达到目标时间。用户必须登录网站并按计划的时间签到。

I'm building a web app that coaches people to rise earlier, it generates a rising schedule for the user over a seventy day period. They input their current rising time and their target rising time. The rising time then diminishes a set amount on a weekly basis until it reaches the target time. The user has to login to the website and 'check in' at their scheduled time.

我对如何生成此计划感到有些困惑,考虑到当前登录用户的时区和夏令时。

I'm at a bit of a loss as to how I generate this plan, taking into account the timezone and DST of the currently logged in user.

我的起始时区是中立 UTC,这是生成计划的代码,(相当详细但是我仍处于实验阶段。)

My starting timezone is the 'neutral' UTC, here's the code for generating the plan, (quite verbose but I'm still in the experimental stages.)

date_default_timezone_set('UTC');


function timeDiff($normRisingTime, $targetRisingTime)
{
    $normRisingTime = strtotime($normRisingTime);
    $targetRisingTime = strtotime($targetRisingTime);
    $timeDiff = $normRisingTime - $targetRisingTime;
    return $timeDiff;
}


function dropPerWeek($numSeconds)
{
    $incDrop = $numSeconds / 9; //9 out of ten weeks the time will decrement and remain the same in week 10
    return $incDrop;
}



$timeDiffSecs = timeDiff("07:00","06:00"); //times for testing purposes



$decrementSecs = dropPerWeek($timeDiffSecs);

$startDate = time();

$dayNum = 1;
for($i = 0; $i < 9; $i++)
{

    $weekNum = $i + 1;  
    for($j = 0; $j < 7; $j++){
        $dayTimeStamp = $startDate + 86400 * $j;
        $dayTimeStamp = round($dayTimeStamp/60)*60; //round to nearest minute
        $dayTot = $j + $dayNum;

        if(empty($dayArray)){
            $dayArray = array(1=>$dayTimeStamp);

        }else{
            $dayArray[] = $dayTimeStamp;
        }

    }

    $dayNum = $dayTot + 1;
    $startDate = $dayTimeStamp + 86400 - $decrementSecs;
}

for($j = 0; $j < 7; $j++){
    $dayTimeStamp = $startDate + 86400 * $j;
    $dayTot = $j + $dayNum;
    $dayArray[] = $dayTimeStamp;
}


foreach ($dayArray as $key=>$value ) {
    echo $key." ".$value." ";
    echo strftime('%c', $value)."<br/>";
}

这给出了所需的输出:

1 1300695300 Mon Mar 21 08:15:00 2011
2 1300781700 Tue Mar 22 08:15:00 2011
3 1300868100 Wed Mar 23 08:15:00 2011
4 1300954500 Thu Mar 24 08:15:00 2011
5 1301040900 Fri Mar 25 08:15:00 2011
6 1301127300 Sat Mar 26 08:15:00 2011
7 1301213700 Sun Mar 27 08:15:00 2011
8 1301299680 Mon Mar 28 08:08:00 2011
9 1301386080 Tue Mar 29 08:08:00 2011
10 1301472480 Wed Mar 30 08:08:00 2011
11 1301558880 Thu Mar 31 08:08:00 2011
12 1301645280 Fri Apr 1 08:08:00 2011

我计划将这些生成的时间戳存储在我的数据库中,并根据用户设置的时区即时进行转换。
但是,当我采用这些动态生成的时间戳并将其显示在DST运行的区域时,会出现问题,这是欧洲/贝尔法斯特的输出

I plan to store these generated timestamps in my database and convert them on the fly depending on the user's set timezone. However a problem arises when I take these dynamically generated timestamps and display them in an area where DST is in operating, here's the output for Europe/Belfast

1 1300695480 Mon Mar 21 08:18:00 2011
2 1300781880 Tue Mar 22 08:18:00 2011
3 1300868280 Wed Mar 23 08:18:00 2011
4 1300954680 Thu Mar 24 08:18:00 2011
5 1301041080 Fri Mar 25 08:18:00 2011
6 1301127480 Sat Mar 26 08:18:00 2011
7 1301213880 Sun Mar 27 09:18:00 2011 //schedule jumps ahead by an hour because of DST
8 1301299860 Mon Mar 28 09:11:00 2011
9 1301386260 Tue Mar 29 09:11:00 2011
10 1301472660 Wed Mar 30 09:11:00 2011
11 1301559060 Thu Mar 31 09:11:00 2011
12 1301645460 Fri Apr 1 09:11:00 2011

随着DST于3月27日生效,我的日程安排提前了一个小时。这是生成此计划的错误方法吗?我如何创建一个时间表,该时间表即使在夏令时(DST)上每周也会减少一定的数量,并且如果用户在时间表中突然移动国家/地区,则可以灵活地转换为另一个时区。

As DST comes into operation on the 27 Mar, my schedule jumps ahead an hour. Is this the wrong way to generate this schedule? How do I create a schedule that diminishes by a set amount each week even over DST and can be flexible enough to be converted into another timezone if the user suddenly moves country during the course of the schedule.

任何指针将不胜感激。

推荐答案

您可以检查 date(' I',$ timestamp)如果时间是DST,则返回1,否则返回0,如果是,则进行一些数学运算。

You can check date('I',$timestamp) which returns 1 if the time is DST and 0 if not, and do some math if so.

这篇关于生成适用于不同时区和DST的计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆