I created a commented script for you. Are there several ways to make this code ok? This is just one example.
<?php
$start = "12:00";// essa é a variável que vem do banco de dados
$next = explode(":",$start); //separa a hora dos minutos
$hour = (int)$next[0] + 2; // adiciona 2 horas
$go = $hour.":".$next[1]; // cria a variável final
?>
<script type="text/javascript">
window.onload = function(){
var go = "<?php echo $go; ?>";
// essa função verifica a cada 10 milisegundos se o horario final é = o horario presente
var stat = setInterval(function(){ atualiza(go) }, 10);
function atualiza(go){
var data = new Date();
var now = data.getHours() + ":" + data.getMinutes();
// se for igual ou maior
if(now >= go){
window.location.href = "http://answall.com"; // redireciona
clearInterval(stat);// limpa
}
}
}
</script>
EDITION
As it was put in the comments, so that the user does not Burle the rules, I re-made the script with php always rescuing the dates. That is, it will be rescued by the server. So it would look like this:
<?php
/*
a variável start terá que ter esse formato:
0000/00/00 00:00 (ano/mes/dia hora:minutos)
isso indicará a data e hora do inicio que o usuário começou a usar o serviço
*/
$start = "2017/04/09 14:55";// essa é a variável que vem do banco de dados
$go = date('Y/m/d H:i', strtotime('+ 2 hours 30 minutes', strtotime($start))); // cria a variável final com 2 horas e meia a mais
?>
<script type="text/javascript">
window.onload = function(){
var go = "<?php echo $go; ?>";
// essa função verifica a cada 10 segundos se o horario final é = o horario presente
var stat = setInterval(function(){ atualiza(go) }, 10);
function atualiza(go){
var data = new Date();
var now = data.getHours() + ":" + data.getMinutes();
// se for igual
if(now >= go){
window.location.href = "http://answall.com"; // redireciona
clearInterval(stat);// limpa
}
}
}
</script>
And 2h and 30 min? Can’t add minutes?
– thecreator
what if the user has a different time on their computer? It affects something?
– thecreator
of yes... I will change..
– Andrei Coelho
I’ll comment on all this.
– Andrei Coelho
OK, thank you very much !
– thecreator
There it is. See that the $start variable has to be set in that date format ok? Then you will need to change the database or create a new column. Hug!
– Andrei Coelho
Don’t forget to approve the answer if it helped you.
– Andrei Coelho
sorry, yesterday I didn’t come here anymore, I’ll approve and thank you very much for everything!
– thecreator