search

Home  >  Q&A  >  body text

Laravel implements daily event loop

Anyone know how to implement a loop of daily events. In my case I have 10 events Think of it as event 1, event 2, event 3 and ....

How can I do it, let's say

  1. Monday Event 1
  2. Tuesday Event 2
  3. Wednesday Activity 3
  4. Thursday Event 4
  5. Friday Event 5
  6. Saturday Event 6
  7. Sunday Event 7
  8. Monday Event 8
  9. Tuesday Event 9
  10. Wednesday Event 10
  11. Thursday Activity 1 (Cycle)
  12. Friday Activity 2 etc

Is there any way to achieve something like this?

P粉609866533P粉609866533242 days ago481

reply all(2)I'll reply

  • P粉573809727

    P粉5738097272024-03-29 14:37:30

    Perform the following operations.

    $day = date("D");  
       switch($day){
         case 'Monday':
              return 'event 1';
         break;
         
         case 'Tuesday':
              return 'event 2';
         break;
          *********
          *********
         case 'Friday':
              return 'event 5';
         break;
         }

    reply
    0
  • P粉130097898

    P粉1300978982024-03-29 12:13:44

    In Laravel you can use Carbon

    You can refer to the following code

    $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;

    result

    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

    reply
    0
  • Cancelreply