2013-05-06 42 views
0

我試圖從文本文件中獲取一些日程安排時間並顯示它們。我在下面的代碼中做了這部分。但我真正需要做的,而不是一直顯示,是將文本文件中的行與當前時間進行比較,並只顯示下一個計劃的時間。我不太清楚如何做到這一點。將文本文件中的行與當前時間進行比較

我想我必須將每一行從字符串轉換爲時間,然後將它們與當前時間進行比較,並在當前時間之後顯示下一個。我不知道該怎麼做。任何幫助,將不勝感激。

<?php 

$file_handle = fopen("schedule.txt", "rb"); 
date_default_timezone_set('America/Denver'); 
$currenttime = date('H:i:s'); 

while (!feof($file_handle)) { 
    $line_of_text = fgets($file_handle); 
    $parts = explode('=', $line_of_text); 

    print $parts[0] . $parts[1]. "<BR>"; 
} 

fclose($file_handle); 
?> 
<p>The current time is:</p> 
<?php 
print $currenttime;  
?> 
+1

你應該張貼的輸入文件 – alfasin 2013-05-06 18:52:44

+0

文字磚的例子內容僅僅是這樣(但換行): 10:00 14:00 16:00 20:00 – Neve 2013-05-06 18:59:30

回答

0

由於文本文件就是:10:00 14:00 16:00 20:00 你應該用 '的探索()函數=' '而不是(空間)'。

0
$lines = file('schedule.txt', FILE_IGNORE_NEW_LINES); 
//sort($lines);//sort $lines if not already sorted 
date_default_timezone_set('America/Denver'); 
$currenttime = date('H:i:s'); 


$line = current($lines); 
while($line !== false && $line < $currenttime) { 
    $line = next($lines); 
} 

if($line > $currenttime) { 
    echo $line; 
} else { 
    echo 'no next one found'; 
} 
+0

哇!你真了不起!謝謝! – Neve 2013-05-06 20:25:25

+0

謝謝。如果這對你有效,你可以考慮[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)答案 – 2013-05-06 20:35:30

相關問題