Printing off a random quote on a page is useful (or at least interesting), but it is nice to rotate them slower than every page view.
A better solution is to use a time based value to work out which quote to display. In this way the quote is changed every hour/day/week or whatever time period you have selected.
Create a file called quote.txt in the same directory as the script and put a single quote on each line.
quote 1 quote 2 quote 3
The following function will take a time part as a single parameter and return a quote.
function quoteByInterval($timePart){ // Make sure it is a integer // Load the quotes file // How many quotes are there in the file? // Figure out the posision of the quote $position = ($timePart % $quoteCount); // Return the quote return $quotes[$position]; }
To get the time parts you can use the PHP date() function with different parameters. Here are some examples.
To use the function just give it a date part, it will then figure out the rest. Here are some examples.
0 comments:
Post a Comment