Read text from a file and load a new string from a row every 24 hour

拥有回忆 提交于 2019-12-13 04:34:19

问题


After some hours of research I made this script:

function get_string_on_interval($strings, $interval) {
    return $strings[round((time()/$interval - floor(time()/$interval)) * sizeof($strings))];
}

echo get_string_on_interval(file('matlista.txt'), 60*60*24);

In this matlista.txt I have a specific word on each row/line. This script shows a string from this text file on my site but the problem is that it doesn't start showing from index 1 (which is row/line 1) it starts from the line 46. I don't know what I'm doing wrong but I want this script to read from line 1 and after 24 hours it should go to line 2 and so on and display the different lines every 24 hour. This script starts to show line 46 and continues from there, I can't find the problem.


回答1:


$data = array(
'line1',
'line2',
'line3'
);

function get_string_on_interval($strings, $interval)
{
   $startTime = '2014-03-11 18:14:00';
   return $strings[floor((time() - strtotime($startTime)) / $interval) 
                % count($strings)]; 
}

echo get_string_on_interval($data, 10);

And $startTime is the datetime from which you want to 'count' the lines (when the first line is shown). Example should change the line every 10 seconds.



来源:https://stackoverflow.com/questions/26724941/read-text-from-a-file-and-load-a-new-string-from-a-row-every-24-hour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!