每天晚上数一个数字


Every night count an number

对于Visual Basic中的项目,我需要一个站点(脚本),它每天晚上都会将站点上的数字重置为1。

因此,如果是00:00,它会将页面上的数字重置为1,并每天重复。

我试过这个代码:

<?php
//Kijken of het 00:00 is
if(date('H') == '00'){
//Het is 00:00 uur dus ik geef 1 weer
  $tijd = 1;
} else {
//Het is nog geen 00:00 uur dus ik geef 0.
  $tijd = 0; 
}
if($_GET["tijd"]){
    $tijd = $_GET["tijd"];
}
//het bestand schrijven
$file = 'nulofeen.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current = $tijd;
// Write the contents back to the file
file_put_contents($file, $current);
//Open het bestand
$file = file("nulofeen.txt");
//Lees de laaste regel uit
for ($i = count($file)-1; $i < count($file); $i++) {
//echo de laaste regel
  echo $file[$i];
}
?>

但这不会起作用——它不会重置任何东西。

您需要设置一个Cron job。这将每晚运行您的脚本,从那里您可以在文件中存储数字1。此外,无需运行时间检查,只需在午夜运行cron即可。

Cron语法,以防您不知道:

00 00 * * * php path/to/your/script.php

它将在每天早上午夜运行一个文件。这假设您使用的是PHP,并且您在linux上运行。

OP在他的评论中提出了另一点,这可能会对未来的读者有所帮助。除了您自己之外的其他人将查看您的网站,因此您需要一种方法在页面加载时自动加载当前数值。要做到这一点,可以设置一个数据库并更新午夜显示的数字,也可以在每次加载页面时从文本文件中读取(请参阅OP)。

创建一个每晚午夜运行的cronjob:

00 0 * * 0,1,2,3,4,5,6 php /path/to/your/script.php

该脚本每次运行时都会重写到文件"1"

<?php
$file_name = "test.txt";
$fh = fopen($file_path, "w") or die("cant open file");
$toFile = "1";
fwrite($fh, $toFile);
fclose($fh);

?>