Need to read line by date

if there is an ex file (news.txt)

and I need php to read my words in this file only once a day

every day php reads only one line →

I am writing this code, but it does not work →> any help

 $wordfile = "words.txt";
 $open = fopen($wordfile, "r");
 $read = fread($open, filesize($wordfile));
 fclose($wordfile);
 $array = explode("\n",$read);
 $date = date("z");
 while ($date++){
echo $array;
 }
+3
source share
3 answers
$f = file("words.txt");
echo $f[date("z")];

It is so simple if you have a file with 366 lines. The problem with your code is that the loop never ends. You probably wanted to while ($date--)do something in a loop that changed the "current line".

+1
source

The following code at runtime displays a record by line number [days from 7/13/2010]

<?php

$startDate = mktime(0,0,0,7,13,2010);
$currentDate = time();
$dateDiff = $currentDate - $startDate;

$dayOn = floor($dateDiff/(60*60*24));

$lines = file("words.txt");

echo trim($lines[$dayOn]) . "\n";

?>
0
source

, file(), :

$wordfile = "words.txt";
$lines = file( $wordfile );
$count = count($lines);
for ($i = 0; $i < $count; $i++) {
    echo 'Line ',($i + 1),': ',$lines[$i],'<br />';
}

, , . , :

$wordfile = "words.txt";
$lines = file( $wordfile );
$count = count($lines);
$day_of_week = date('z'); // 0 (for Sunday) through 6 (for Saturday)
echo $lines( $day_of_week );

If you need a more complex solution, you can change the line $day_of_week = date('z');to suit your needs. Learn more about the date()PHP function .

0
source

Source: https://habr.com/ru/post/1754403/


All Articles