Php 如何将此更改为每周而不是每次刷新?

Php 如何将此更改为每周而不是每次刷新?,php,Php,我有一些代码对我来说运行良好。不过,我现在希望对其进行修改,以使其不是在每次刷新页面时都进行更改,而是在周一每周只刷新一次 每次刷新时更改它的代码是: <center><?php ?><div style="min-height: 75px; padding-top: 0px; padding-left: 5px; padding-right: 5px;"> <?php $file = "/pathway/to/the_file/on_

我有一些代码对我来说运行良好。不过,我现在希望对其进行修改,以使其不是在每次刷新页面时都进行更改,而是在周一每周只刷新一次

每次刷新时更改它的代码是:

<center><?php
    ?><div style="min-height: 75px; padding-top: 0px; padding-left: 5px; padding-right: 5px;">
    <?php
$file  = "/pathway/to/the_file/on_the_server/theme_name/file_name.txt";
$quote = file($file);
echo $quote[array_rand($quote)];
?></div></center>
现在,通过这个例子,似乎需要知道“图像”的数量。因为我使用的是一个不断添加的引号文件,所以我不想计算有多少引号,也不想在每次向文件添加更多引号时不断更改此代码

有没有一种方法可以改变这个例子,使之与我所拥有的、正在努力实现我所追求的目标相适应,或者我在这方面走的不是正确的道路

提前感谢您的输入。

您可以使用来确定上次更改文件的时间。文件上说“时间以a的形式返回”,这意味着它以秒为单位。您可以使用获取当前时间。您可以使用获取一周中的某一天。您可以使用和获取并写入“当前报价文件”的内容

为了保证稳定性,最好检查

为了进一步解释“在最后修改日期之前或之前的星期一”,考虑一下你的站点星期四、星期二访问的情况。周四,您将创建一个新的当前报价文件,周二距离上次更新该文件还不到一周,但我们希望使其看起来像周一更新的一样。通过查看上次修改时间之前的星期一,我们将看到自那天起已经一周了,因此我们将创建一个新的当前报价文件


您可以通过删除小时和分钟部分来更好地实现这一点,因此它总是在周一午夜“更新”,但我太累了,无法编写它,您应该能够使用
date()
,或者使用一些巧妙的除法和乘法来解出它。

您可以这样做。您可以将照片文件名和上次更改日期存储在一个文件中。在周一,照片会改变并记录到文件中。使用array_rand可能会导致每周重复使用。您可以轻松添加额外的逻辑,以确保阵列中的新照片与前一周不同。希望有帮助

if (date('l') == 'Monday') {
   $aFileContents = file('photoDate.txt');

   if($aFileContents[1] == date('Y-m-d')) {
      $photo = $aFileContents[0];
   } else {
      $photo = array_rand($aPhotoArray);
      $fp = fopen('photoDate.txt', 'w');
      fwrite($fp, $photo . '\n');
      fwrite($fp, date('Y-m-d'));
      fclose($fp);
   }
} else {
   $aFileContents = file('photoDate.txt');
   $photo = $aFileContents[0];
}

换行符需要双引号(
“\n”
,而不是
“\n”
)。感谢您提供的所有精彩信息!对于您显示的第二个代码,它是否会每周更改(而不是针对周一,只是一周中的任何时间)?我不知道为什么要引用两个文件而不是一个?@user2376452我想到了一个更好的方法。这个新版本的效果与每周一更新一次相同,但不要求任何人在周一实际访问该页面。我希望它的工作原理足够清楚。有两个文件的原因是,一个包含当前选择的引号(
$currentQuoteFile
),另一个包含可能引号的完整列表(
$quoteFile
)。感谢您的精彩解释!!这是否假定我正在手动更改当前报价文件?我目前使用1个随机抽取的文件。提供的代码能否从1个文件中随机抽取?@user2376452查看以
if($pickNewQuote)
开头的代码。这就是它创建和更新
$currentQuoteFile
的方式。大概
$quoteFile
将是您已经拥有的文件。
$currentQuoteFile = "current-quote.txt";
$quoteFile = "all-quotes.txt";

if (!file_exists($currentQuoteFile)) {
    // Create a new "current quote file" if it doesn't exist yet.
    $pickNewQuote = true;
} else {
    $lastModified = filemtime($currentQuoteFile);

    $oneDayInSeconds = 60 * 60 * 24;
    $oneWeekInSeconds = $oneDayInSeconds * 7;

    // Look for the Monday on or before the last modified date. This lets us
    // emulate "updating on Monday" without actually requiring someone to visit
    // this page every Monday.
    $lastModified -= (date("N", $lastModified) - 1) * $oneDayInSeconds;

    // If the current time is at least a week later than the Monday on or
    // before the quote file was updated, we need to pick a new quote.
    $pickNewQuote = time() - $oneWeekInSeconds > $lastModified;
}

if ($pickNewQuote) {
    $quotes = file($quoteFile);
    $quote = $quotes[array_rand($quotes)];
    file_put_contents($currentQuoteFile, $quote);
} else {
    $quote = file_get_contents($currentQuoteFile);
}
if (date('l') == 'Monday') {
   $aFileContents = file('photoDate.txt');

   if($aFileContents[1] == date('Y-m-d')) {
      $photo = $aFileContents[0];
   } else {
      $photo = array_rand($aPhotoArray);
      $fp = fopen('photoDate.txt', 'w');
      fwrite($fp, $photo . '\n');
      fwrite($fp, date('Y-m-d'));
      fclose($fp);
   }
} else {
   $aFileContents = file('photoDate.txt');
   $photo = $aFileContents[0];
}