実際、これは、タイムゾーンオフセットを使用することにより、サーバー側のコードなしでJavaScriptのみを使用して実現できます。
使用できる関数は次のとおりです。
var onAir = function (day, start, end, timezone) {
var local, utc, show, days, onAir, startValues, endValues, startTime, endTime, startMinutes, endMinutes, showMinutes;
// by default, we are not on air
onAir = false;
// map day numbers to indexes
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Firday', 'Saturday'];
// convert start/end times to date objects
startValues = start.split(':');
endValues = end.split(':');
startTime = new Date();
endTime = new Date();
startTime.setHours(startValues[0], startValues[1]);
endTime.setHours(endValues[0], endValues[1]);
// add the hours minutes together to get total minutes
startMinutes = (startTime.getHours() * 60) + startTime.getMinutes();
endMinutes = (endTime.getHours() * 60) + endTime.getMinutes();
// get the current local time
local = new Date();
// get the current time in the show's timezone
utc = local.getTime() + (local.getTimezoneOffset() * 60000);
show = new Date(utc + (3600000*timezone));
// convert the show hours + minutes to just minutes
showMinutes = (show.getHours() * 60) + show.getMinutes();
// test to see if the show is going on right now
if (days[show.getDay()] === day && (showMinutes >= startMinutes && showMinutes <= endMinutes)) {
onAir = true;
}
return onAir;
}
// example: Air time is Tuesday between 1-2pm Central Time (-6)
var texasShowOnAir = onAir('Tuesday', '13:00', '14:00', '-6'));
// now check if we are on air
if (texasShowOnAir) {
// do stuff here...
}
これで、この関数を次のように使用できます。
var check = onAir('DAY', 'STARTTIME', 'ENDTIME', 'YOURTIMEZONE');
これにより、が返されtrue/false
ます。必ず24時間形式を使用してください。
多くの場合(特に共有ホスティングを使用している場合)、サーバーを自分とは異なるタイムゾーンに設定できるため、これはサーバーのタイムスタンプを使用するよりも優れているとさえ言えます。
これがデモフィドルです:http: //jsfiddle.net/stevenschobert/mv54B/