Redirect user to a certain time

0

Good evening everyone, I wonder if there is any way to for example:

I have the following table in my database:

  

table.videos

id | starttime

1  | 12:00

and after two half hours, that is at 2:30 p.m. automatically the user is redirected to another page ..

Ex: the user is on the videos.php page and exactly at 2:30 p.m. (ie, two hours more than what is set in the database) the user is automatically redirected

    
asked by anonymous 09.04.2017 / 04:55

1 answer

0

I've created a commented script for you. There are several ways to make this code okay? This is just an 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://pt.stackoverflow.com"; // redireciona
            clearInterval(stat);// limpa
        }
    }
}

</script>

EDITION

As it was put in the comments, so that the user does not circumvent the rules, I re-did the script with php always rescuing the dates. That is, it will be rescued by the server. Then 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://pt.stackoverflow.com"; // redireciona
            clearInterval(stat);// limpa
        }
    }
}

</script>
    
09.04.2017 / 21:57