2017-02-15 17 views
0

我有4個電子郵件地址的數組。每個星期二我需要發送一封電子郵件,所以我使用CRON運行一個php腳本。該腳本包含一組電子郵件地址。第1周我發送一封電子郵件到數組[0],第2周我發送電子郵件到數組[1],第3周我發送一封電子郵件到數組[2]和第4周我發送一封電子郵件到數組[3]。然後我再說一遍,所以第5周我會發送一封電子郵件到數組[0],等等變成無窮大。如何按4周的週期每週發送一次電子郵件給4人中的1人?

如何在date()上計算腳本運行時發送的郵件?我能做的唯一事情就是在開始日期後的未來10年填寫一個日期查找數組,然後進行查找,但這似乎效率低下。

如果明年有獎勵積分,我可以將其他用戶添加到陣列中,而不會影響到目前的訂單。

+0

參見http://stackoverflow.com/questions/9567673/get-week-number-in-the-year-from-a-date-php – carebdayrvis

+0

'回波日期('W');''會給你當年的週數;其餘的由你決定。 – MonkeyZeus

回答

1

如果我理解正確的,你沒有問題發送而是,堅持一定的順序排列。那就是說,你不是在錯誤的地方尋找解決方案嗎?

爲什麼不簡單地將數組的索引號存儲在您發送電子郵件的人的某處(例如,在一個純文件中)?並在執行cron時(每個星期二)自動更新該文件?這允許您添加另一個用戶而不會影響訂單。

見下文實例代碼:

// The list with users 
$userList = [ 
    0 => "[email protected]", 
    1 => "[email protected]" 
]; 

// Determine the user who received last email 
$lastUsedIndex = 1; // e.g. extract this input from a file 

// Determine the last user in the list 
$maxUserIndex = max(0, count($userList) - 1); 

// Determine who is next in line to receive the email 
$newIndex = (++$lastUsedIndex <= $maxUserIndex ? $lastUsedIndex : 0); 
$reciever = $userList[$newIndex]; 

// Send the email 

// Update the source containing the last receiver (which is the value of $newIndex) 
+0

我在日期上做了一些數學工作,這個解決方案從來沒有出現在我身上。感謝您指出。 –

+0

選擇此作爲答案,因爲它也獲得獎勵積分。再次感謝。 –

1

您可以使用一些模塊化的數學運算來實現簡單的循環體系。

另外,還可以使用date("W")

假設你只有3個陣列得到週數:

<?php 
$emails = [ 
    ['[email protected]', '[email protected]', '[email protected]'], 
    ['[email protected]', '[email protected]', '[email protected]'], 
    ['[email protected]', '[email protected]', '[email protected]'], 
]; 
$roundRobinSize = count($emails); 
$thisWeekEmailsList = $emails[(intval(date("W")) - 1) % $roundRobinSize]; 
someFunctionForSendingMail($thisWeekEmailsList, 'Subject', 'Message'); 

此代碼,如果最終你添加另一個列表可以得到錯誤的數組。這是因爲它基於週數實施了簡單的循環賽。

+0

感謝您使用此解決方案。 –

相關問題