开发者

Help with recurring javascript countdown and timezones

开发者 https://www.devze.com 2023-02-06 22:00 出处:网络
Goal: To create a countdown to our next available live stream. Details: We live stream six times a week all (PST).

Goal: To create a countdown to our next available live stream.

Details: We live stream six times a week all (PST).

1. Sunday at 8:00 a.m.

2. Sunday at 10:00 a.m.

3. Sunday at 12:00 p.m.

4. Sunday at 6:30 p.m.

5. Wednesday at 7:00 p.m.

6. Saturday at 10:00 a.m.

My approach: Originally I check what day it is and what time it is then create the countdown to the next stream. This works fine for our timezone (PST) and any that are relatively close.

Problem: Checking first the day breaks my approach for people in timezones that put them a day ahead of us. For example if someone from London checks the site on Thursday at 2:30 a.m. (GMT) my code would start the countdown to the Saturday 10:00 a.m. stream instead of the Wednesday at 7:00 p.m. (PST) which would start in 30 mins.

I'm sure what I have done can be cleaned up and improved, tell me how.

My code:

// Countdown
function countDown( addDay, hour, min ) {
    var newCount = new Date(); 
    newCount = new Date(newCount.getFullYear(), newCount.getMonth(), newCount.getDate() + addDay, hour, min); 
    $('#service_countdown').countdown({until: $.countdown.UTCDate(-8, newCount)}); 
}

var day_ref, day, addDay, hour, min;
addDay = 0;
hour = 0;
min = 0;
day = {};

day_ref = new Date();
day = {"day" : day_ref.getDay(), "hour" : day_ref.getHours(), "min" : day_ref.getMinutes()};

// It's Sunday
if ( day.day === 0 ) {

    if ( day.hour < 8 ) {
          hour = 8;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 12 ) {
          hour = 12;
        countDown(addDay, hour, min);
    }
    else if ( day.hour <= 18 && day.min < 30 ) 开发者_StackOverflow社区{
          hour = 18;
           min = 30;
        countDown(addDay, hour, min);
    } 
    else {
        addDay = 3;
          hour = 19;
        countDown(addDay, hour, min);
    }
} 
// It's Monday
else if ( day.day === 1 ) {
    addDay = 2;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Tuesday
else if ( day.day === 2 ) {
    addDay = 1;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Wednesday
else if ( day.day === 3) {

    if ( day.hour < 19 ) {
          hour = 19;
        countDown(addDay, hour, min);
    } else {
        addDay = 3;
          hour = 10;
        countDown(addDay, hour, min);
    }
} 
// It's Thursday
else if ( day.day === 4 ) {
    addDay = 2;
      hour = 10;
    countDown(addDay, hour, min);
} 
// It's Friday
else if ( day.day === 5 ) {
    addDay = 1;
      hour = 10;
    countDown(addDay, hour, min);
} 
// All that's left is Saturday
else {

    if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } else {
        addDay = 1;
          hour = 8;
        countDown(addDay, hour, min);
    }
}

Thanks in advance for any help!


I ended up just doing all the time calculating server side. With the help of bob-the-destroyer the working php is quite compact given the requirements.

<?php

$schedule = array(
    'this Sunday 8am',
    'this Sunday 10am',
    'this Sunday 12pm',
    'this Sunday 6:30pm',
    'this Wednesday 7pm',
    'this Saturday 10am'
    );

$current_time = strtotime('now');
foreach ($schedule as &$val) {
    $val = strtotime($val);
    // fix schedule to next week if time resolved to the past
    if ($val - $current_time < 0) $val += 604800; 
    }
sort($schedule);
$countdown = $schedule[0] - $current_time;

?>

<script type="text/javascript">
    var myTime = <?php echo $countdown; // just personally prefer full tags ?>;
    $('#countdown').countdown({ until: myTime}); 
</script>
0

精彩评论

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