Tim Van Damme
Tim Van Damme

Reputation: 1

Script to redirect to a HTML page between certain times

I am stuck with the following: I have a custom html/php loader on a joomla cms that loads a page with specific information. This information renews 4 times a day, at 02:00 08:00 14:00 and 20:00 local time. I have created 4 pages (1 for each renewal) so that people can compare (via frames).

Now what I would like to do is to create a html or php (if needed with java) page that redirects to one of those four specific pages according the time the page is opened.

If the time is between 02:00 and 08:00 it should open "00.html"

If the time is between 08:00 and 14:00 it should open "06.html"

If the time is between 14:00 and 20:00 it should open "12.html"

If the time is between 20:00 and 02:00 it should open "18.html"

those html files are frames, with one frame containing a menu of the available information called 'mainmenu', a frame containing the seclected information via the menu called 'showmodel' and a frame to select the 00/06/12/18 page if desired. But I would love the fact that the visitor will get the latest information at the time he or she opens the main page... The date does not matter. It is only the time that matters.

Upvotes: 0

Views: 157

Answers (4)

Tim Van Damme
Tim Van Damme

Reputation: 1

This is what I have made of it with your kind help... if i am not mistaken it will check the hour at server-side right?

<?php
$hours = date("H"); // 00 - 23

if (($hours >= 0) && ($hours < 2)) {
    header('location: 12.html');
}
elseif (($hours >= 2) && ($hours < 8)) {
    header('location: 18.html');
}
elseif (($hours >= 8) && ($hours < 14)) {
    header('location: 00.html');
}
elseif (($hours >= 14) && ($hours < 20)) {
    header('location: 06.html');
}
elseif (($hours >= 20) && ($hours < 0)) {
    header('location: 06.html');
}
?>

Upvotes: 0

Obmerk Kronen
Obmerk Kronen

Reputation: 15949

Not tested and not exactly what asked but something like this should work i think

$hour= date("H");
header('location: '.$hour.'.html');

Just wanted to üpoint out that you can use variables inside header. It makes things maybe more flexible to set conditional later ..

Upvotes: 1

Jim_M
Jim_M

Reputation: 273

Since you have a range of hours in which you want to perform a specific redirect, you can make that almost as granular as you want by using a switch statement. A switch statement will examine your value, and then find a "case" that meets the criteria and execute that code.

http://php.net/manual/en/control-structures.switch.php

Please remember, when doing a redirect, no other output can have been made.

$hour = (int)date('H');
switch ($hour) {
    case ($hour >= 0 && $hour <= 2):
        header('location: 18.html');
        break;
    case ($hour > 2 && $hour <= 8):
        header('location: 00.html');
        break;
    case ($hour > 8 && $hour <= 14):
        header('location: 06.html');
        break;
    case ($hour > 14 && $hour <= 20):
        header('location: 12.html');
        break;
    case ($hour > 20):
        header('location: 18.html');
        break;
    default:
        header('location: default.html');
        break;
}
exit;

Redirect docs :http://php.net/manual/en/function.header.php

Upvotes: 1

Rick
Rick

Reputation: 33

This example must help you :)

    <?php
    $hours = date("H"); // 00 - 23

    if (($hours >= 2) && ($hours <= 8)) {
        header('location: 00.html');
    }

    ?>

Upvotes: 1

Related Questions