Ver Mensaje Individual
  #3 (permalink)  
Antiguo 01/04/2013, 12:38
Avatar de marlanga
marlanga
 
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 3 meses
Puntos: 206
Respuesta: Cuenta regresiva con hora del servidor

Código Javascript:
Ver original
  1. (function($) {
  2.                 $.fn.countdown = function(options, callback) {
  3.                    
  4.                     //custom 'this' selector
  5.                     thisEl = $(this);
  6.                    
  7.                     var last=$.now();
  8.                     var acumulated=0;
  9.                    
  10.                     //array of custom settings
  11.                     var settings = {
  12.                         'date': null,
  13.                         'date2': null,
  14.                         'format': null
  15.                     };
  16.                    
  17.                     //append the settings array to options
  18.                     if(options) {
  19.                         $.extend(settings, options);
  20.                     }
  21.                    
  22.                     //main countdown function
  23.                     function countdown_proc() {
  24.                        
  25.                         eventDate = Date.parse(settings['date']);
  26.                        
  27.                         if (settings['date2'])
  28.                         {
  29.                             acumulated+= $.now() - last;
  30.                             currentDate = Date.parse(settings['date2'])+acumulated;
  31.                             last=$.now();
  32.                         }
  33.                         else
  34.                         {
  35.                             currentDate = Math.floor($.now());
  36.                         }
  37.                        
  38.                        
  39.                         if(eventDate <= currentDate) {
  40.                             callback.call(this);
  41.                             clearInterval(interval);
  42.                         }
  43.                        
  44.                         seconds = Math.floor((eventDate - currentDate)/1000);
  45.                        
  46.                         days = Math.floor(seconds / (60 * 60 * 24)); //calculate the number of days
  47.                         seconds -= days * 60 * 60 * 24; //update the seconds variable with no. of days removed
  48.                        
  49.                         hours = Math.floor(seconds / (60 * 60));
  50.                         seconds -= hours * 60 * 60; //update the seconds variable with no. of hours removed
  51.                        
  52.                         minutes = Math.floor(seconds / 60);
  53.                         seconds -= minutes * 60; //update the seconds variable with no. of minutes removed
  54.                        
  55.                         //conditional Ss
  56.                         if (days == 1) { thisEl.find(".timeRefDays").text("day"); } else { thisEl.find(".timeRefDays").text("days"); }
  57.                         if (hours == 1) { thisEl.find(".timeRefHours").text("hour"); } else { thisEl.find(".timeRefHours").text("hours"); }
  58.                         if (minutes == 1) { thisEl.find(".timeRefMinutes").text("minute"); } else { thisEl.find(".timeRefMinutes").text("minutes"); }
  59.                         if (seconds == 1) { thisEl.find(".timeRefSeconds").text("second"); } else { thisEl.find(".timeRefSeconds").text("seconds"); }
  60.                        
  61.                         //logic for the two_digits ON setting
  62.                         if(settings['format'] == "on") {
  63.                             days = (String(days).length >= 2) ? days : "0" + days;
  64.                             hours = (String(hours).length >= 2) ? hours : "0" + hours;
  65.                             minutes = (String(minutes).length >= 2) ? minutes : "0" + minutes;
  66.                             seconds = (String(seconds).length >= 2) ? seconds : "0" + seconds;
  67.                         }
  68.                        
  69.                         //update the countdown's html values.
  70.                         if(!isNaN(eventDate) || !isNaN(currentDate)) {
  71.                             thisEl.find(".days").text(days);
  72.                             thisEl.find(".hours").text(hours);
  73.                             thisEl.find(".minutes").text(minutes);
  74.                             thisEl.find(".seconds").text(seconds);
  75.                         } else {
  76.                             alert("Invalid date. Here's an example: 12 Tuesday 2012 17:30:00");
  77.                             clearInterval(interval);
  78.                         }
  79.                     }
  80.                    
  81.                     //run the function
  82.                     countdown_proc();
  83.                    
  84.                     //loop the function
  85.                     interval = setInterval(countdown_proc, 1000);
  86.                    
  87.                 }
  88.             }) (jQuery);
  89.             $(function(){
  90.            
  91.                 $("#countdown").countdown({
  92.                 //date: "17 august 2013 13:00:00",
  93.                 //date2: "17 august 2013 12:00:00",
  94.                 date: "<?php echo date("d F Y",strtotime($fecha_finaliza)); ?> <?php echo $reg["hora_finaliza"] ?> ",
  95.                 date2: "<?php echo date("d F Y",strtotime($fecha_actual)); ?> <?php echo $reg["hora_actual"] ?> ",
  96.                 format: "on"
  97.                 },
  98.  
  99.                 function() {
  100.                 // callback function
  101.  
  102.                 });
  103.             });