Redirect on Specific Date/Time

Hi, I would like to redirect to a different page on a specific date/time, preferably using PHP.

I would like the redirect to change, say from page1.html to page2.html, on Friday March 21, 2014 at 18:20 GMT.

Is this very complex to carry out?

I searched on Google but there didn’t seem to be a strong consensus on the best way to do it.

Cheers,

Leao

  1. Redirect when an user gets to your page in that exact time
<?php
if( date('Y-m-d H:i') == '2013-03-21 18:20' ) {
    header('Location: page2.html');
    exit;
}

Now, I hope that your page (page1.html is from a rewrite rule or knows to execute the PHP code - HTML will not)

OR

  1. You will need an Ajax request, so you can get the server time while the user is on your “page1.html” page.
    Make few requests and check the time then redirect. This is not so simple as the first code but also not so complicated.

OR

  1. You can make a client-side redirect.

<script>
var d = new Date();
if( d.getFullYear() == 2013 && d.getMonth() == 3 && d.getDate() == 21 && ... hour and minute ) {
    window.location.href = 'page2.html';
}

Check here the Date object

OR just make change after specified date/time.

<?php
//March 21, 2014 at 18:20 GMT
date_default_timezone_set('GMT');
$target = strtotime("March 21, 2014, 18:20");
$date = strtotime(date("F j, Y, g:i a"));
$page = ($date>=$target ? "page2.html" : "page1.html");
//// $page can be used as you wish.  Assuming header location.///
//IMPORTANT:  This must be at the top of your page before anything is sent to browser!
header("location: $page");
exit;
?>

With this, you may get into a redirect loop.

Haa, ha Good spot vectorialpx. I suppose you could put this ONLY on page1.html assuming an html page is going to read php in the first place.

<?php
//March 21, 2014 at 18:20 GMT
date_default_timezone_set('GMT');
$target = strtotime("March 21, 2014, 18:20");
$date = strtotime(date("F j, Y, g:i a"));
//IMPORTANT:  This must be at the top of your page1.html before anything is sent to browser!
if($date>=$target){
	header("location: page2.html");
	exit;
}
?>

Thank you very much Drummin and vectorialpx

It would probably be better to make a page called index.php that has this code.

<?php
//March 21, 2014 at 18:20 GMT
date_default_timezone_set('GMT');
$target = strtotime("March 21, 2014, 18:20");
$date = strtotime(date("F j, Y, g:i a"));
$page = ($date>=$target ? "page2.html" : "page1.html");
include($page);
?>