Skip to content

Instantly share code, notes, and snippets.

@kprimdal-dk
Last active September 24, 2019 19:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kprimdal-dk/4701412 to your computer and use it in GitHub Desktop.
Save kprimdal-dk/4701412 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.countdown = function(options, callback) {
//custom 'this' selector
thisEl = $(this);
//array of custom settings
var settings = {
'date': null,
'format': null
};
//append the settings array to options
if(options) {
$.extend(settings, options);
}
//main countdown function
function countdown_proc() {
eventDate = Date.parse(settings['date']) / 1000;
currentDate = Math.floor($.now() / 1000);
if(eventDate <= currentDate) {
callback.call(this);
clearInterval(interval);
}
seconds = eventDate - currentDate;
days = Math.floor(seconds / (60 * 60 * 24)); //calculate the number of days
seconds -= days * 60 * 60 * 24; //update the seconds variable with no. of days removed
hours = Math.floor(seconds / (60 * 60));
seconds -= hours * 60 * 60; //update the seconds variable with no. of hours removed
minutes = Math.floor(seconds / 60);
seconds -= minutes * 60; //update the seconds variable with no. of minutes removed
//conditional Ss
if (days == 1) { thisEl.find(".timeRefDays").text("dag"); } else { thisEl.find(".timeRefDays").text("dage"); }
if (hours == 1) { thisEl.find(".timeRefHours").text("time"); } else { thisEl.find(".timeRefHours").text("Timer"); }
if (minutes == 1) { thisEl.find(".timeRefMinutes").text("Minut"); } else { thisEl.find(".timeRefMinutes").text("Minutter"); }
if (seconds == 1) { thisEl.find(".timeRefSeconds").text("Sekund"); } else { thisEl.find(".timeRefSeconds").text("Sekunder"); }
//logic for the two_digits ON setting
if(settings['format'] == "on") {
days = (String(days).length >= 2) ? days : "0" + days;
hours = (String(hours).length >= 2) ? hours : "0" + hours;
minutes = (String(minutes).length >= 2) ? minutes : "0" + minutes;
seconds = (String(seconds).length >= 2) ? seconds : "0" + seconds;
}
//update the countdown's html values.
if(!isNaN(eventDate)) {
thisEl.find(".days").text(days);
thisEl.find(".hours").text(hours);
thisEl.find(".minutes").text(minutes);
thisEl.find(".seconds").text(seconds);
} else {
alert("Invalid date. Here's an example: 12 Tuesday 2012 17:30:00");
clearInterval(interval);
}
}
//run the function
countdown_proc();
//loop the function
interval = setInterval(countdown_proc, 1000);
}
}) (jQuery);
<script>
$(document).ready(function(){
$("#countdown").countdown({
date: "<?php $tomorrow = strtotime('tomorrow 12pm'); echo date( 'd F Y H:i:s', $tomorrow ); ?>",
format: "on"
}
});
</script>
@aznoisib
Copy link

/**

  • Convert date function
    */
    $select.convert = function(diff,maxdate){
var cxx = $select.data.interger.seconds,
    ccs = $select.data.interger.intervale*cxx,
    cnm = $select.data.interger.hoursandmins*$select.data.interger.intervale,
    crh = $select.data.interger.hoursandmins*$select.data.interger.hoursandmins*$select.data.interger.intervale,
    cyd = $select.data.interger.hoursandmins*$select.data.interger.hoursandmins*$select.data.interger.intervale*$select.data.interger.days,
     


 daysmax = [(((diff%(cyd))%(crh))%(cnm))/ccs,((diff%(cyd))%(crh))/(cnm)*cxx,(diff%(cyd))/(crh)*cxx,diff/(cyd)*cxx],
 hoursmax = [((diff%(crh))%(cnm))/ccs,(diff%(crh))/(cnm)*cxx,diff/(crh)*cxx],
 minutesmax = [(diff%(cnm))/ccs,diff/(cnm)*cxx],  
 secondsmax = [diff/ccs],  
    //console.log(daysmax);
    convert = (maxdate=='days') ? daysmax
   : (maxdate=='hours') ? hoursmax
   : (maxdate=='minutes') ? minutesmax
   : (maxdate=='seconds') ? secondsmax
   :              false;
 
return convert;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment