Hello,
I am trying to make a fake product sales value counter, which will update it self to certain amount not more than $100 per day i.e in 24hrs.
To make this even simple here an Ex: - <?php
-
$amt = 100000;
-
srand(floor(time() / (60*60*24)));
-
$total = $amt + rand() % 100;
-
echo "$". $total ."/-";
-
$amt = $total;
-
?>
How ever the code above causes to roll back or reduce to previous days value.
day1 = 100087
day2 = 100083 .this is the issue.
it should be 100087 + 83 = 100170
4 2886 Atli 5,058
Expert 4TB
That code hard-codes the $amt value to 100000 on every run. If you want it to increase on yesterday's value, you need to store it somehow.
The simplest way would be to dump it into a text file on every run and read it back the next run. Check out the file_get_contents and file_put_contents functions for that.
For example, modifying your previous example: -
<?php
-
// The location of the file to use to store
-
// the previous amount
-
$dataFile = "amt.txt";
-
-
// If the file doesn't exist, default to
-
// the inital 100000.
-
if (!file_exists($dataFile)) {
-
$amt = 100000;
-
}
-
else {
-
// Otherwise read the previous value from
-
// the file.
-
$amt = (int) file_get_contents($dataFile);
-
}
-
-
// Generate the new value...
-
srand(floor(time() / (60*60*24)));
-
$total = $amt + rand() % 100;
-
echo "$". $total ."/-";
-
-
// And dump it back into the file.
-
if (!file_put_contents($dataFile, $total)) {
-
// If it fails to write to the fle, you'll
-
// want to know about it...
-
echo "Failed to save the new total!";
-
}
-
?>
-
That's about as simple as it gets.
Hello Atli,
Thank you for your reply, is their any other way then using files, also the value stored into the file is over written very time.
I am fare new to files and php, yet i will try to run your solution.
Atli,
the above code generates random value after every page refresh, how do i correct the code so that the addition happens once 24hrs, and i am okay to save the value into the file.
Please advice.
Atli 5,058
Expert 4TB
You'll either have to save the time of the last update with the value, so that you can compare it to the current time when before you updated it, or you can just use the filemtime() function you can get a timestamp for when the file was last modified. You can then use that to find out if a day has passed, and if not exit the script.
Fox example: -
// Subtract the timestamp 24 hours ago from the
-
// last time the file was edited, to get the
-
// number of seconds still remaining until the
-
// file can be edited again.
-
$seconds = filemtime($dataFile) - strtotime("now - 24 hours");
-
-
// If the seconds are more than 0, there is
-
// still time remaining until it can be edited.
-
if ($seconds > 0) {
-
echo "You can only update the file once every 24 hours. ";
-
echo "Time until next update: ";
-
echo date("H:i:s", $seconds );
-
exit;
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sonoman |
last post by:
Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I...
|
by: VAL |
last post by:
How can I make an application form that generates a unique number starting
from 1001.?
|
by: Locky |
last post by:
Sorry if this is a repost - my connection went as I was posting the
1st time!
I want to insert a random number, in mod 10, in a field, based on
whether another field is empty or not.
I am...
|
by: quickcur |
last post by:
Suppose I have a function rand() that can generate one integer random
number between 0 and 100. Suppose also rand() is very expensive. What
is the fastest way to generate 10 different random number...
|
by: Peteroid |
last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible...
|
by: Papajo |
last post by:
Hi, This script will write a random number into a document write tag,
I've been trying to get it to write into a input form box outside the
javascript, any help is appreciated. Thanks Joe
...
|
by: RadiationX |
last post by:
I have a problem that I really don't understand at all. In my previous
post I could get started on my projects I just had a few problems with
syntax errors. This problem is something that I don't...
|
by: gagan.singh.arora |
last post by:
Hi there.
I want to generate random numbers with a given probability, say 80%
even and 20% odd. Is it possible to implement such an algorithm in C?
|
by: urkel |
last post by:
Hello everybody,
I have these errors on C program that may be because of random number definition. I use an SSH to compile C like in Linux. May be you have idea whether the errors are due to the...
|
by: Anil Gupte |
last post by:
I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:
public string GetRand(int count)
{
string number = "";
for (int i=0;...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |