Jump to content

Recommended Posts

Hi Everyone,

 

I currently have the following script which randomly displays a message every time a user visits/refreshes a webpage. I would like to adjust it to display a new random message every 24 hours. The problem I am having is how to get the time from the server and tell the script to update every 24 hours.

 

Any suggestions are welcome.

 

Thanks in advance,

 

kaiman

 

<?php
// this script will randomly display a featured hike on each page visit

// randomly displays hikes
srand ((double) microtime() * 1000000);
$featuredhike = rand(0,count($hike)-1);

echo ($hike[$featuredhike]);

// echo to page
$hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n";
$hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n";
$hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n";
$hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n";
$hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n";
$hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n";
?> 

Thanks mikesta707,

 

I would rather just keep in all in one file rather then quering a database. Also, I was just reading about cron jobs and it seems they can be pretty server intensive so I am reluctant to use that approach...

 

How could I grab the server time and then use PHP operators to tell it to update itself every 24 hours?

 

I am struggling with the logic on how to do this...

 

Thanks!

Can't really do that. PHP can get the time, and can get the time that is 24 hours from then, but you can't tell the page to execute something in 24 hours, unless you manually load the page every 24 hours. if you are only doing 1 CRON a day, (especially one that only updates 1 row of a table) you won't have to worry about server load

I found this which essentially does what I want simply using PHP:http://www.phpfreaks.com/forums/index.php?topic=157119 and http://stackoverflow.com/questions/66422/how-do-i-rotate-an-image-at-12-midnight-every-day

 

The question is how would I simply use it for the text in the file in my first post?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.