Search code examples
phplaravellaravel-controller

Is it possible to redirect your Laravel blade at a particular time?


Please am trying to redirect a page in Laravel7 once a time is clocked e.g once the time is 04:05 the page should redirect.

I tried this below but it didn't work.

 public function countdown(){

        $currentdate = date("d-m-Y h:i:s");

        $futuredate = "02-05-2020 18:4:25";

        if($currentdate == $futuredate){
            return redirect()->route('welcome');
        }

    }

Can anyone help on how I can achieve this?


Solution

  • You need to do this on the client side, this can't be done on the server side.

    You can use setInterval to execute a piece of code that will check if the time is 04:05 and then redirect the page using window.location.href = "/yourroute";

    Something like this:

    setInterval(function () {
      var date = new Date();
      var hour = date.getHours();
      var minute = date.getMinutes();
    
      if (hour === 4 && minute === 5) {
        location.href = "/welcome"
      }
    }, 60000);
    

    60000 is 1 minute in millieseconds.