Redirect transponder at a certain time

Asked

Viewed 47 times

0

Good evening to all, I wonder if there is some way for example:

I have the following table in my database:

table videos.

id | starttime

1  | 12:00

and after two half hours, ie at 14:30 automatically the user is redirected to another page..

Ex: the user is in the videos.php page and exactly at 14:30 (that is, two more hours than defined in the database) the user is automatically redirected

1 answer

0


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?

  • what if the user has a different time on their computer? It affects something?

  • of yes... I will change..

  • I’ll comment on all this.

  • OK, thank you very much !

  • 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!

  • Don’t forget to approve the answer if it helped you.

  • sorry, yesterday I didn’t come here anymore, I’ll approve and thank you very much for everything!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.