2015-02-09 47 views
0

我有一個包含了一週的小時,分​​鍾和秒的天(0-6)和時間如下數組:從一週的日期和時間工作了將來的日期在PHP

$times = array( array("1","10:04:00"), 
        array("1","14:20:00"), 
        array("1","20:30:00"), 
        array("1","22:22:00"), 
        array("2","10:14:00"), 
        array("2","14:36:00"), 
        array("2","20:12:00"), 
        array("2","22:42:00"), 
        array("3","12:04:00"), 
        array("3","15:20:00"), 
        array("3","21:30:00"), 
        array("4","23:22:00"), 
        array("4","09:04:00"), 
        array("4","12:20:00"), 
        array("4","19:30:00"), 
        array("4","22:32:00"), 
        array("5","13:04:00"), 
        array("5","16:20:00"), 
        array("5","20:10:00"), 
        array("5","22:02:00") 
        ); 

我怎樣才能循環上述內容並計算出事件的下一個日期? 例如,如果今天是星期二,上述陣列中的第幾日期爲:

下週一在10:04:00 下週一十四時20分00秒 下週一在20時30分○○秒 下週一22點22分零零秒 今天在10:14:00 今天在14時36分00秒 等等

一旦循環到達上述數組的末尾,它會回送到一開始就需要制定未來的日期。日期和時間將需要轉換爲unix日期時間。

這是迄今爲止該腳本:

$row = 1; 
if (($handle = fopen("evergreen.csv", "r")) !== FALSE) { 
     $loopy = 0; 
     $thisWeek = date("W"); 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     if(!isset($times[$loopy])) {$loopy = 0;} 
     $dayoftheweek = $times[$loopy][0]; 
     $thetime  = $times[$loopy][1]; 
     echo "Day: $dayoftheweek - The time: $thetime<br />"; 
     // How can I work out the next date and time to post from this? 
     $loopy++; 
    } 
    fclose($handle); 
} 

我無法工作,如何實現以上。任何幫助,將不勝感激。

謝謝!

+0

只是編寫一個函數並循環遍歷數組,這並不是什麼大問題。 – Daan 2015-02-09 10:13:28

+0

是的,循環很簡單。它將星期幾和時間轉換成unix日期,並且不管它是過去還是將來。我會在這個問題上更清楚一點。 – iagdotme 2015-02-09 10:16:09

回答

0

要操縱日期,您可以使用strtotime()。直接從php.net網站的例子:

<?php 
echo strtotime("now"), "\n"; 
echo strtotime("10 September 2000"), "\n"; 
echo strtotime("+1 day"), "\n"; 
echo strtotime("+1 week"), "\n"; 
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n"; 
echo strtotime("next Thursday"), "\n"; 
echo strtotime("last Monday"), "\n"; 

在你的情況,你會放在第二個參數的時間:

strtotime("+1 day", $timefromloop); 
0

就 「問」 strtotimeDateTime

<?php 
$weekdays = array(
    '1'=>'monday', 
    '2'=>'tuesday', 
    '3'=>'wednesday', 
    '4'=>'thursday', 
    '5'=>'friday' 
); 
$currentDay = getdate()['wday']; // for php < 5.4 there is no function array de-reference 

$times = array(
    array("1","10:04:00"), 
    array("1","14:20:00"), 
    array("1","20:30:00"), 
    array("1","22:22:00"), 
    array("2","10:14:00"), 
    array("2","14:36:00"), 
    array("2","20:12:00"), 
    array("2","22:42:00"), 
    array("3","12:04:00"), 
    array("3","15:20:00"), 
    array("3","21:30:00"), 
    array("4","23:22:00"), 
    array("4","09:04:00"), 
    array("4","12:20:00"), 
    array("4","19:30:00"), 
    array("4","22:32:00"), 
    array("5","13:04:00"), 
    array("5","16:20:00"), 
    array("5","20:10:00"), 
    array("5","22:02:00") 
); 

$times = array_map(function($e) use ($weekdays, $currentDay) { 
     $q = $weekdays[ $e[0] ]; // get the name of the weekday for strtotime 
     $q.= ' '.$e[1]; // append the time of the event 

     if ($e[0]==$currentDay) { 
      return strtotime($q); 
     } 
     else { 
      // for any other day than today "ask" strtotime for the next/coming day with that name 
      return strtotime('next '.$q); 
     } 
    }, 
    $times 
); 


foreach($times as $ts) { 
    echo date('Y-m-d H:i:s', $ts), "\r\n"; 
} 

打印(今日)

2015-02-09 10:04:00 
2015-02-09 14:20:00 
2015-02-09 20:30:00 
2015-02-09 22:22:00 
2015-02-10 10:14:00 
2015-02-10 14:36:00 
2015-02-10 20:12:00 
2015-02-10 22:42:00 
2015-02-11 12:04:00 
2015-02-11 15:20:00 
2015-02-11 21:30:00 
2015-02-12 23:22:00 
2015-02-12 09:04:00 
2015-02-12 12:20:00 
2015-02-12 19:30:00 
2015-02-12 22:32:00 
2015-02-13 13:04:00 
2015-02-13 16:20:00 
2015-02-13 20:10:00 
2015-02-13 22:02:00