Different redirects per day of the week

Asked

Viewed 39 times

1

<?php

 $site[] = 'youtube.com';
 $site[] = 'facebook.com';

$destino = rand(0, (count($site) - 1));

header("Location: " . $site[$destino]);
?>

Guys, I use this script to do a random redirect. The script will choose between the two variables and will redirect, simple. But I wanted help to change this per day. Example, on Monday the variables would be

$site[] = 'youtube.com';
$site[] = 'facebook.com';

On Tuesday would be

$site[] = 'exemplo.com';
$site[] = 'mangaonli.com';

And so on. Someone could help me?

1 answer

4

First you will make an array, with the days of the week in English containing an array of links for each day of the week, here in the example I am using the first 3 letters because I will use date("D"), but it could be the full name to use date("l") or even numbers of 0-6 to use date("w"):

$dias_e_links=array(
    "Mon"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
    "Tue"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
    "Wed"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
    "Thu"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
    "Fri"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
    "Sat"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
    "Sun"=>array(
        "link1.com",
        "link2.com",
        "link3.com"
    ),
);

Now just take today and give the rand() at the links:

$hoje = date("D");
$destino = rand(0, (count($dias_e_links[$hoje]) - 1));
header("Location: " . $dias_e_links[$hoje][$destino]);

Remembering that it is only one of the possible ways, but I found the simplest

Documentation of the date function()

  • Thanks, man. Thanks a lot!

  • If you can give me a hand with this I’d like to thank you, dear:https://answall.com/questions/380999/aleat%C3%B3rio-entre-Hor%C3%A1rio

Browser other questions tagged

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