Jump to content


Photo

Changing a number every 24 hours

text change every 24 hours

Best Answer AbraCadaver, 23 September 2013 - 04:17 PM

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;
Go to the full post


  • Please log in to reply
17 replies to this topic

#1 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 23 September 2013 - 02:47 PM

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



#2 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 02:52 PM

Where do the numbers come from?


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#3 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 23 September 2013 - 02:55 PM

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.   :-\



#4 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 02:56 PM

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?


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#5 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 23 September 2013 - 03:01 PM

Any random number between 50 and 5000.  

 

I don't understand what you're asking about parameters.  I'm sorry.



#6 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 03:12 PM

Well, I meant the rules or conditions pertaining to the numbers.  I guess I'm trying to get more information.  When does the 24 hours start?  Midnight?  Noon?  Another time?


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#7 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 03:31 PM

Can the numbers be reused later?


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#8 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,537 posts

Posted 23 September 2013 - 03:34 PM

Is this number a part of a date? You could look into using the date function

http://php.net/date

 

You need to give us more info of what this number does.



#9 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 23 September 2013 - 03:39 PM

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.  



#10 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 03:42 PM

So you are going to use a random number for the number of changes?  So this is not meant to be accurate?  Just something to get them to download the pdf?


mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#11 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,537 posts

Posted 23 September 2013 - 03:42 PM

Still need more info. What changes? 



#12 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 23 September 2013 - 03:57 PM

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, 23 September 2013 - 03:58 PM.


#13 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 04:07 PM

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;

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#14 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,537 posts

Posted 23 September 2013 - 04:08 PM

:shrug: You've lost me.

date('z');

will give you the day of the year. It'll reset to 1  on 1st Jan 2014 tho.



#15 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,775 posts
  • LocationBonita, FL

Posted 23 September 2013 - 04:10 PM

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.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#16 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,889 posts
  • LocationThe Republic of Texas

Posted 23 September 2013 - 04:17 PM   Best Answer

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;

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#17 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 24 September 2013 - 08:34 AM

Oh thank you so much everyone!  I'm off to try this and I'll be back.

 

You people have always been to nice and kind!  



#18 flipper828

flipper828

    Member

  • Members
  • PipPip
  • 29 posts

Posted 24 September 2013 - 09:19 AM

Thank you so much Kicken and AbraCadaver for your generosity!  Surprise....I've not hear of mt_rand and mt_srand in my research and studies.  Thank you for the links.  I want to go back and study these soon.  






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com