2015-09-06 67 views
0

我會開始說我不是程序員。像很多人一樣,我可以從PHP,JS,PowerShell等的示例中一起破解一些東西,但我在這裏丟了一個空白。PHP - 比較日期和時間陣列/數據庫(不是文字日期)

我正在嘗試創建一個頁面,顯示當前每日任務在無盡任務2中的情況。這些任務每18小時更換一次,每週在同一天/時間重複發生。

下面的工作,但它是...糟糕。

<!DOCTYPE html> 
<html> 
<body> 

<?php 

$h = date('G'); //set variable $h to the server hour of the day, "G" is the date key for hours in 24 format (not 12), with no leading 0s, like 02. Adjust hourly offset in the event of daylight savings problems.. i.e $h = $h-2; 
$h = $h+7; 
$d = date('w'); //set variable $d to the server day of the week, 0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday 

// Monday before 1400 
if ($d == 1 && $h < 14) $text = 'Ossuary: Sanguine Fountains [Heroic]'; 

// Monday after 1400 
if ($d == 1 && $h >= 14) $text = 'Ossuary: Resonance of Malice [Heroic]'; 

// Tuesday before 0800 
if ($d == 2 && $h < 8) $text = 'Ossuary: Resonance of Malice [Heroic]'; 
// Tuesday after 0800 
if ($d == 2 && $h >= 8) $text = 'Ssraeshza Temple [Heroic]'; 

// Wednesday before 0200 
if ($d == 3 && $h < 2) $text = 'Ssraeshza Temple [Heroic]'; 
// Wednesday after 0200 but before 2000 
if ($d == 3 && $h >= 2 && $H < 20) $text = 'Ssraeshza Temple: Inner Sanctum [Heroic]'; 
// Wednesday after 2000 
if ($d == 3 && $h >= 20) $text = 'Brokenskull Bay: Bilgewater Falls [Heroic]'; 

// Thursday before 1400 
if ($d == 4 && $h < 14) $text = 'Brokenskull Bay: Bilgewater Falls [Heroic]'; 
// Thursday after 1400 
if ($d == 4 && $h >= 14) $text = 'Brokenskull Bay: Hoist the Yellow Jack [Heroic]'; 

// Friday before 0800 
if ($d == 5 && $h < 8) $text = 'Brokenskull Bay: Hoist the Yellow Jack [Heroic]'; 
// Friday after 0800 
if ($d == 5 && $h >= 8) $text = 'Zavithloa: The Lost Caverns [Heroic]'; 

// Saturday before 0200 
if ($d == 6 && $h < 2) $text = 'Zavithloa: The Lost Caverns [Heroic]'; 
// Saturday after 0200 but before 2000 
if ($d == 6 && $h >= 2 && $H < 20) $text = 'Zavithloa: The Hidden Caldera [Heroic]'; 
// Saturday after 2000 
if ($d == 6 && $h >= 20) $text = 'Castle Highhold [Heroic]'; 

// Sunday before 0200 
if ($d == 0 && $h < 2) $text = 'Castle Highhold [Heroic]'; 
// Sunday after 0200 but before 2000 
if ($d == 0 && $h >= 2 && $H < 20) $text = 'Castle Highhold: Thresinets Den [Heroic]'; 
// Sunday after 2000 
if ($d == 0 && $h >= 20) $text = 'Ossuary: Sanguine Fountains [Heroic]'; 

echo "Current Daily Mission: $text <BR>"; 
?> 

</body> 
</html> 

如果它是一個日期,我可能把它在一個SQL表,然後依次通過做DATEDIFF,但我不知道如何與它在當前的格式工作..我d也更喜歡把它放在一個數組中並循環遍歷,但是不能確定使用它的最佳方式。我應該使用任務名稱作爲鍵,然後找到一種方法來分隔開始和結束時間嗎?但由於我以任意的方式存儲信息,我不確定如何與一天/每天的時間進行比較?

$MissionTimes = array(
0 => "Mission1:1-14-00:2-8-00", 
1 => "Mission2:2-8-00:3-2-00", 
2 => "Mission3:3-2-00:3-20-00", 
3 => "Mission4:3-20-00:4-14-00", 
4 => "Mission5:4-14-00:5-8-00", 
5 => "Mission6:5-8-00:6-2-00", 
6 => "Mission7:6-2-00:6-18-00", 
7 => "Mission8:6-18-00:0-2-00", 
8 => "Mission9:0-2-00:0-18-00", 
9 => "Mission10:0-18-00:1-14-00", 
); 

任何幫助指針/建議將非常感激。

編輯.... 隨着埃弗茨的幫助,我現在有一個數組,這樣的作品,任何人都可以幫助我如何獲得「未來」的結果,以及最後一個循環功能?我認爲這樣做的唯一方法就是添加結束時間,然後使用for循環來檢查當前日期/時間是否在當前的每日時間之間,但是當星期六的任務落在時間之後,這會崩潰(第6天)和星期天(第0天)。

<!DOCTYPE html> 
<html> 
<body> 

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

// Daily "start" time listed below in GMT, each daily lasts 18 hours. 

// Sunday (0) 2AM = Castle Highhold: Thresinet’s Den [Heroic] 
// Sunday (0) 8PM = Ossuary: Sanguine Fountains [Heroic] 
// Monday (1) 2PM = Ossuary: Resonance of Malice [Heroic] 
// Tuesday (2) 8AM = Ssraeshza Temple [Heroic] 
// Wednesday (3) 2AM = Ssraeshza Temple: Inner Sanctum [Heroic] 
// Wednesday (3) 8PM = Brokenskull Bay: Bilgewater Falls [Heroic] 
// Thursday (4) 2PM = Brokenskull Bay: Hoist the Yellow Jack [Heroic] 
// Friday (5) 8AM = Zavith’loa: The Lost Caverns [Heroic] 
// Saturday (6) 2AM = Zavith’loa: The Hidden Caldera [Heroic] 
// Saturday (6) 8PM = Castle Highhold [Heroic] 

$MissionTimes = array(
array( 
     'missionid' => "Castle Highhold: Thresinet’s Den [Heroic]", 
     'startdow' => 0, 
     'starthour' => 0 
    ), 
array( 
     'missionid' => "Castle Highhold: Thresinet’s Den [Heroic]", 
     'startdow' => 0, 
     'starthour' => 2 
    ), 
array( 
     'missionid' => "Ossuary: Sanguine Fountains [Heroic]", 
     'startdow' => 0, 
     'starthour' => 20 
    ), 
array(
     'missionid' => "Ossuary: Resonance of Malice [Heroic]", 
     'startdow' => 1, 
     'starthour' => 14 
    ), 
array( 
     'missionid' => "Ssraeshza Temple [Heroic]", 
     'startdow' => 2, 
     'starthour' => 8 
    ), 
array( 
     'missionid' => "Ssraeshza Temple: Inner Sanctum [Heroic]", 
     'startdow' => 3, 
     'starthour' => 2 
    ), 
array( 
     'missionid' => "Brokenskull Bay: Bilgewater Falls [Heroic]", 
     'startdow' => 3, 
     'starthour' => 20 
    ), 
array( 
     'missionid' => "Brokenskull Bay: Hoist the Yellow Jack [Heroic]", 
     'startdow' => 4, 
     'starthour' => 14 
    ), 
array( 
     'missionid' => "Zavith’loa: The Lost Caverns [Heroic]", 
     'startdow' => 5, 
     'starthour' => 8 
    ), 
array( 
     'missionid' => "Zavith’loa: The Hidden Caldera [Heroic]", 
     'startdow' => 6, 
     'starthour' => 2 
    ), 
array( 
     'missionid' => "Castle Highhold [Heroic]", 
     'startdow' => 6, 
     'starthour' => 20 
    ) 
); 

$hour = date('G'); 
$dow = date('w'); 
$hour = $hour+7; 

foreach($MissionTimes as $mission) 
{ 
    if ($mission['startdow'] > $dow) 
    { 
    // If the current date of the week is higher than the start dow of 
    // of this mission, we skip to the next item. 
    continue; 
    } 
    if ($mission['starthour'] > $hour) 
    { 
    // If the current hour of the day is higher than the start hour of 
    // of this mission, we skip to the next item. 
    continue; 
    } 

    $missionstring = $mission['missionid']; 
// break; 
} 

echo "The current mission is: $missionstring"; 
?> 

</body> 
</html> 

回答

0

我覺得你非常接近,但我會改變結構有點不包含所有內容到一個單一的字符串。

例子:

$MissionTimes = [ 
    [ 
    'missionid' => 1, 
    'startdow' => 1, 
    'starthour' => 14 
    ], 
    [ 
    'missionid' => 1, 
    'startdow' => 1, 
    'starthour' => 14 
    ], 
    [ 
    'missionid' => 2, 
    'startdow' => 2, 
    'starthour' => 8 
    ], 
    /*** etc ***/ 
]; 

那麼幾個音符:

  1. 我分手了所有的值分爲3分不同的。
  2. 我刪除了「一週的結束日期」和「結束時間」,因爲據我所知,它總是與下一個任務的開始時間相同,所以我們不需要兩次。與array(and)相同。

那你如何找到目前的任務?

$hour = date('G'); 
$dow = date('w'); 

$missionId = 0; 

foreach($MissionTimes as $mission) { 
    if ($mission['startdow']) > $dow) { 
     // If the current date of the week is higher than the start dow of 
     // of this mission, we skip to the next item. 
     continue; 
    } 
    if ($mission['starthour'] > $hour) { 
     // If the current hour of the day is higher than the start hour of 
     // of this mission, we skip to the next item. 
     continue; 
    } 

    // If we got here, stop the loop. 
    $missionId = $mission['missionid']; 
    break; 
} 

2個注意事項:

  1. 我沒有測試此代碼。
  2. 您需要確保您的$MissionTimes陣列處於有序狀態,並且從星期日(小時0,第0天)午夜開始的條目開始。

最後,你沒有提及任何關於時區的信息,但你給小時值增加了7個小時......這很奇怪。我的假設是,任務任務時間以UTC表示,而你在太平洋時間。要獲取UTC的值,請使用gmdate()而不是date()函數。

+0

這工作很好,但我不得不刪除中斷,由於某種原因,它不喜歡你的數組的格式,一旦我明白它是什麼(一個多維數組),我做了一點搜索。現在我要做的就是弄清楚如何確定下一個任務是什麼。 我給這個值增加了7個小時,因爲我只知道變化的GMT時間,我不記得服務器在什麼時區,當我找到它時,我會根據需要更改時間並刪除小時修改器。 – trulyvexed

+0

查看我的最新評論。使用'gmdate()'而不是盲目地添加小時(無論如何,在DST改變之後這將不起作用)。此外,數組問題可能是因爲您使用的是過時的PHP版本。 – Evert

+0

這不會讓我感到驚訝。我正在使用Bluehosts,因爲我剛剛開始涉足Wordpress。通過phpinfo()來判斷它是PHP 5.2.17版 - 再次感謝您的幫助。很高興有一個工作解決方案。 – trulyvexed