I would like to know how I can make the script below work as follows in case it is a countdown timer, but in case it counts up a date posted on it and comparing it with the current date by counting in the case there When it reaches 0
, the message appears.
The case is that I would like to know how to put this script to work as follows, it doing a 30 second count every time the page is updated after 30 seconds to appear the text being that I need it to do this calculation as I said taking the current date and if it updates the page as said the count returns again.
Below the scripts
index.html
<!doctype html>
<html>
<head>
<title>Contador em PHP</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Requisicao AJAX
var requisicao = function(){
$.ajax({
url: "contador.php"
}).done(function(resultado){
// Exibe o resultado no elemento com ID contador
$("#contador").html(resultado);
});
};
// Executa a requisicao com intervalo de 100ms
setInterval(requisicao, 100);
});
</script>
</head>
<body>
<p>Faltam <span id="contador"></span> segundos para o fim do mundo!</p>
</body>
</html>
counter.php
<?php
// Define as datas
$data_atual = date('d-m-Y h:i:s');
$data_final = date('2015-12-21');
// Converte as datas para a hora UNIX e realiza o calculo da diferenca
$diferenca = strtotime($data_final) - strtotime($data_atual);
// Exibe o resultado se ele for positivo. Caso seja negativo, exibe 0.
echo ($diferenca >= 0) ? $diferenca : 0;
?>
jQuery.js too large to be put here.
The reason for the change and adapt it to display URLs.