Question

I have this code on stackoverflow:

    <?php
$links = array(array('url' => 'http://google.com', 'name'=>'google'),
               array('url' => 'http://hotmail.com', 'name' => 'hotmail'),
               array('url' => 'http://hawkee.com', 'name' => 'Hawkee'));
$num = array_rand($links);
$item = $links[$num];

printf('<a href="%s" title="%s">%s</a>', $item['url'], $item['name'], $item['name']);
?>

This code displays a random link. I want to add time to each link, 24 hours after switching to the next link 1, after 24h switch to link 2 and repeat the same function for link 3.

¿ how could I do that process ?

Thanks for your reply.

Was it helpful?

Solution

You could use the current unix timestamp to calculate the index of the link to display.

$item = $links[time()/86400 % 3];

86400 is the number of seconds in a day.

OTHER TIPS

If I understood correctly, get the day of year and use modulo to decide which link should be shown. This way it will be rotated every 3 days. See date function.

$links = array(array('url' => 'http://google.com', 'name'=>'google'),
               array('url' => 'http://hotmail.com', 'name' => 'hotmail'),
               array('url' => 'http://hawkee.com', 'name' => 'Hawkee'));
//$num = array_rand($links);
$num = intval(date('z'))%3 // this will return 0,1,2 based on which day we are currently
$item = $links[$num];

printf('<a href="%s" title="%s">%s</a>', $item['url'], $item['name'], $item['name']);

use php cron job for create php time events look example here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top