每24小时添加一个随机数


Add a random number every 24 hours

我需要一个脚本,每24小时自动添加一个随机数(在1到5之间(并将其存储在文本文件中,然后在24小时后创建一个新的随机数并将其添加到文本文件中的前一个结果中,我设法完成了一些关闭操作,但仍需要刷新按钮才能生效,但我希望它自动完成,所以如果每天有1个用户访问该页面,如果一个人每月访问一次页面,他们应该都看到相同的数字(将从文本文件中读取(。

这是我到目前为止的代码:

<?php
$dataFile = "amt.txt";
$date1 = date("H:i:s");
$date2 = date("H:i:s",filemtime("amt.txt"));
$diff = abs(strtotime($date2) - strtotime($date1));

$secs = floor($diff);

if ($diff < 86400) {
    echo "$date1 'n"; 
echo "$date2 'n";
printf($secs);
    exit;
}   
if (!file_exists($dataFile)) {
    $amt = 0;
}
else {
    // Otherwise read the previous value from
    // the file.
    $amt = (int) file_get_contents($dataFile);
}
// Generate the new value...
$Number = rand(1,5);
$total = $amt + $Number;
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!";
}

?>

基本上,我想显示一个虚假的订阅者数量,它在逻辑上应该随着时间的推移而增加。所以我想每天更新这个数字,因为我的网站上有这个数字作为每月订阅人数,所以当一个用户在任何时候访问网站时,应该会看到与在该确切时间访问网站的任何其他用户相同的数字。希望现在能更清楚。

我首先使用time((而不是date(xxx(,这样您就可以直接获得diff secodns。但我不明白这样做的目的,也不知道你是想附加数字还是覆盖数字,也不清楚数字是否每24小时变化一次,为什么一个月后用户应该得到相同的数字。

$date1 = time();
$date2 = filemtime("amt.txt");
$diff = $date1 - $date2;
if ($diff < 86400)
{
    echo "$date1 'n"; 
    echo "$date2 'n";
    printf($diff);
    exit;
}