Skip to content

Instantly share code, notes, and snippets.

@leowebguy
Created July 13, 2016 13:51
Show Gist options
  • Save leowebguy/3a457410a9833ca844301cc554cbd70a to your computer and use it in GitHub Desktop.
Save leowebguy/3a457410a9833ca844301cc554cbd70a to your computer and use it in GitHub Desktop.
Week Counter in JS+PHP
var EVENTDAY = '.esc_attr($day_number).'; // sunday
var EVENTHOUR = '.esc_attr($hour_number).'; // 9am
function getRemaining( now ) {
if ( now == null ) now = new Date();
var dow = now.getDay();
// the "hour" for now must include fractional parts of the hour, so...
var hour = now.getHours() + now.getMinutes()/60 + now.getSeconds()/3600;
// how many days from current day until event day?
var offset = EVENTDAY - dow;
// if event day is past *OR* if today is the event day but the event time is past...
if ( offset < 0 || ( offset == 0 && EVENTHOUR < hour ) ) {
offset += 7;
}
// so this date (day of the month) is the next occurrence of the event:
var eventDate = now.getDate() + offset;
// and so then next occurrence of the event is at this time:
var eventTime = new Date( now.getFullYear(), now.getMonth(), eventDate, EVENTHOUR, 0, 0 );
// this is how many milliseconds from now to the next event occurrence
var millis = eventTime.getTime() - now.getTime();
// convert milliseconds to days/hours/minutes/seconds:
var seconds = Math.round( millis / 1000 );
var minutes = Math.floor( seconds / 60 );
seconds %= 60;
var hours = Math.floor( minutes / 60);
minutes %= 60;
var days = Math.floor( hours / 24 );
hours %= 24;
if ( days == 7 ) days = "0";
if ( seconds < 10 ) seconds = "0" + seconds;
if ( minutes < 10 ) minutes = "0" + minutes;
if ( hours < 10 ) hours = "0" + hours;
// and return that formatted pretty:
if ( days == 0 ) {
return hours + "h" + minutes + "m" + seconds+ "s";
} else if ( days == 1 ) {
return days + " dia, " + hours + "h" + minutes + "m" + seconds+ "s";
} else {
return days + " dias, " + hours + "h" + minutes + "m" + seconds+ "s";
}
}
function tick() {
document.getElementById("countdown").innerHTML = getRemaining();
var d = new Date();
var weekday = new Array(7);
weekday[0]= "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
var h = d.getHours();
//n = "Tuesday";
//h = "16";
//if (getRemaining().slice(0,10) == "6 dias e 2" || getRemaining().slice(0,11) == "0 dias e 00") {
if (n == "Sunday" && (h == "08" || h == "09" || h == "10" || h == "11" || h == "12" || h == "13")) {
document.getElementById("tcvpb_service_box_countdown").innerHTML = "<style>#countdown-btn-on{display:block!important;}</style>";
} else if (n != "Sunday" && n != "Saturday" && h == "16" ) { //Wed Apr 13 2016 19:06:11 GMT-0400 (EDT)
document.getElementById("tcvpb_service_box_countdown").innerHTML = "<style>#countdown-btn-on-radio{display:block!important;}</style>";
} else {
document.getElementById("tcvpb_service_box_countdown").innerHTML = "<style>#countdown-btn-off{display:block!important;}</style>";
}
}
setInterval( tick, 1000 ); // specifies once a second
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment