Jump to content

Changing a number every 24 hours


Go to solution Solved by AbraCadaver,

Recommended Posts

Hello to everyone!

 

I have a webpage that displays a number.  I want this number to change every 24 hours for anyone that visits the site.  Right now, I am manually changing this number each day.

 

For the last year, I have searched help websites, consulted many many books and consulted with a couple of programmers I know that seem to not even understand what I am talking about.   

 

Would someone give me a hand in learning how to do this?  Your help would be so much appreciated.  To give you an idea of my knowledge level, what I have learned comes from an online php beginner and intermediate class and then some playing aorund and testing from books.  Little, I know, and I apologize in advanced.  

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/282392-changing-a-number-every-24-hours/
Share on other sites

Thank you for being so quick!

 

I just pluck them out of the air when updating the site.  

 

However, I have read something about putting a list of numbers in a .txt file.  There is no database connected to this site so the .txt file (if that is an option) would probably be the easiest.  

 

I hope I am making sense to you.   :-\

Thank you for being so quick!

 

I just pluck them out of the air when updating the site.  

 

However, I have read something about putting a list of numbers in a .txt file.  There is no database connected to this site so the .txt file (if that is an option) would probably be the easiest.  

 

I hope I am making sense to you.   :-\

 

It's pretty easy to do, but can the numbers just be random?  What parameters do they have?  Are they any number between 0 and infinity or what?

Oh Ok.  

 

The numbers can be reused and the ideally, I would like it to change at midnight EST.  

 

The number is not going to do anything but inform the visitor about the number of changes that have happened since the day before.  It will have a hyperlink to a .pdf I have but I know how to do the hyperlink.  

 

Thank you both so much.  I know this has to be frustrating.  

The number does not have to be accurate to the number of changes to a data point spreadsheet I have available each day.  However, that IS a future project.  

 

For now, I just want a different number to display each day which is linked to a .pdf which explains the data points on the spreadsheet.  

 

Does this make more sense?  *fingers crossed*

Edited by flipper828

Not extensively tested, but try:

date_default_timezone_set('America/New_York');
$file = 'number.txt';
$date = $number = 0;

if(file_exists($file)) {
    list($date, $number) = file($file, FILE_IGNORE_NEW_LINES);
}
if((time()) > strtotime('+24 hours', $date)) {
    $date = strtotime('midnight');
    $number = mt_rand(50, 5000);
    file_put_contents($file, "$date\n$number");
}
echo $number;

mt_rand - you can use that to just generate a random number.

mt_srand - This seeds the random number generator with an initial value. Using the same seed will cause mt_rand to generate the same number

date - This will let you get the current date.

 

So, to get a random number that changes each day, seed the number generator with the current date and then call mt_rand.

  • Solution

mt_rand - you can use that to just generate a random number.

mt_srand - This seeds the random number generator with an initial value. Using the same seed will cause mt_rand to generate the same number

date - This will let you get the current date.

 

So, to get a random number that changes each day, seed the number generator with the current date and then call mt_rand.

 

I'll be damned!  :o

date_default_timezone_set('America/New_York');
mt_srand(date('Ymd'));
$number = mt_rand(50, 5000);
mt_srand();  //reset for other calls

echo $number;
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.