问题描述
经过几个小时的研究,我编写了此脚本:
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);
在此matlista.txt
中,我在每行/每行上都有一个特定的单词.该脚本显示了我网站上此文本文件中的字符串,但问题是它没有从index 1
(它是行/行1)开始显示,而是从line 46
开始.我不知道自己在做什么错,但我希望此脚本从line 1
中读取,并且在24小时后应转到line 2
,依此类推,并每24小时显示不同的行.该脚本开始显示line 46
并从那里继续,我找不到问题.
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.
推荐答案
$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);
$startTime
是您要计数"行的日期时间(显示第一行时).示例应每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.
这篇关于每隔24小时从文件中读取文本并从行中加载新的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!