任何人都知道如何實現每日事件的循環。就我而言,我有 10 個事件 將其視為事件 1、事件 2、事件 3 和 ....
我怎樣才能做到,讓我們說
有什麼辦法可以實現這樣的事情嗎?
P粉5738097272024-03-29 14:37:30
執行如下操作。
$day = date("D"); switch($day){ case 'Monday': return 'event 1'; break; case 'Tuesday': return 'event 2'; break; ********* ********* case 'Friday': return 'event 5'; break; }
P粉1300978982024-03-29 12:13:44
在 Laravel 中,您可以使用 Carbon
#您可以參考以下程式碼
$now = Carbon::now(); $weekStartDate = $now->startOfWeek(); $daysInMonth = $weekStartDate->daysInMonth; $events = ["event 1","event 2","event 3","event 4","event 5","event 6","event 7","event 8","event 9","event 10"]; $str = ""; for($i = 0; $i < $daysInMonth; $i++) { $str .= $now->startOfWeek()->add($i, 'day')->isoFormat('dddd') . "_". $events[$i % count($events)]."\n"; } echo $str;
結果
Monday_event 1 Tuesday_event 2 Wednesday_event 3 Thursday_event 4 Friday_event 5 Saturday_event 6 Sunday_event 7 Monday_event 8 Tuesday_event 9 Wednesday_event 10 Thursday_event 1 Friday_event 2 Saturday_event 3 Sunday_event 4 Monday_event 5 Tuesday_event 6 Wednesday_event 7 Thursday_event 8 Friday_event 9 Saturday_event 10 Sunday_event 1 Monday_event 2 Tuesday_event 3 Wednesday_event 4 Thursday_event 5 Friday_event 6 Saturday_event 7 Sunday_event 8 Monday_event 9 Tuesday_event 10 Wednesday_event 1