JavaScript - Cuenta regresiva con hora del servidor y no la del cliente

 
Vista:

Cuenta regresiva con hora del servidor y no la del cliente

Publicado por sebastian (1 intervención) el 01/04/2013 05:14:58
Hola, mi problema es el siguiete: tengo una web donde hay cuenta regresiva de productos. El problema es que el tiempo restante depende del la hora del cliente ya que se hace en javascript.

La solucion seria poner el javascript con la hora del servidor por medio de php o de alguna otra forma. aca dejo el script usado:

intente cambiar la parte donde obtiene la hora actual, pero sin exito.
Especificamente esta linea: currentDate = Math.floor($.now() / 1000);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<script>
(function($) {
$.fn.countdown = function(options, callback) {
 
//custom 'this' selector
thisEl = $(this);
 
//array of custom settings
var settings = {
'date': null,
'date2': 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("day"); } else { thisEl.find(".timeRefDays").text("days"); }
if (hours == 1) { thisEl.find(".timeRefHours").text("hour"); } else { thisEl.find(".timeRefHours").text("hours"); }
if (minutes == 1) { thisEl.find(".timeRefMinutes").text("minute"); } else { thisEl.find(".timeRefMinutes").text("minutes"); }
if (seconds == 1) { thisEl.find(".timeRefSeconds").text("second"); } else { thisEl.find(".timeRefSeconds").text("seconds"); }
 
//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>
<script>


$("#countdown").countdown({
//date: "17 august 2013 12:00:00",
 
date: "<?php echo date("d F Y",strtotime($fecha_finaliza)); ?> <?php echo $reg["hora_finaliza"] ?> ",
format: "on"
},
 
function() {
// callback function
 
});
 
 
</script>


saludos y gracias por tomarse su tiempo!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Cuenta regresiva con hora del servidor y no la del cliente

Publicado por xve (2100 intervenciones) el 01/04/2013 10:24:56
Hola Sebastian, para ello, en una web que hicimos, utilizamos esta función para devolver la hora del lugar de donde se conectaba el cliente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * Función que devuelve la fecha actual aplicando el cambio de horario del cliente
 * Para poder funcionar, tiene que haber un campo oculto con la diferencia en horas
 * con el id "tz".
 * Tiene que recibir:
 *	$format => el formato de datos a devolver:
 *		1- new date()
 * 		2- new Date().getTime();
 */
function tzChange(format)
{
	var tz=($("#idtz").val())*60*60*1000;
	var now=new Date();
 
	if(format==1)
	{
		now.setTime(now.getTime()+tz);
		return now;
	}else if(format==2){
		now.setTime(now.getTime()+tz);
		return now.getTime();
	}
}

En el código web, existía un div oculto con el id="idtz" que contenía la diferencia en horas del utc de nuestro servidor.

Espero que te sirva
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar