开发者

Generating a schedule that works over different timezones and DSTs

开发者 https://www.devze.com 2023-02-17 21:47 出处:网络
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. T

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.

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/>";
}

This gives the desired output of this:

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

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 2开发者_如何学编程011
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

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.

Any pointers would be greatly appreciated.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号