1

Anyone have an idea how can I implement loop of a daily event. In my case, I have 10 event Consider it as event 1, event 2, event 3 and ....

How can I make, let say on

  1. Monday Event 1
  2. Tuesday Event 2
  3. Wednesday Event 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 Event 1 (Loop back)
  12. Friday Event 2 and so on

Is there any way I can implement something like this?

Unkown Kid
  • 135
  • 1
  • 1
  • 8
  • Are these going to be stored in the DB? – nice_dev Jul 13 '22 at 08:37
  • Yes this will be stored in the DB – Unkown Kid Jul 13 '22 at 08:47
  • So like you will store everything at once or store first entry the first day and then run your script the second day for storing the second entry? – nice_dev Jul 13 '22 at 08:52
  • All the event is already in the DB, this event is just to be display to the users so that they know what event for this day. ID 1 for event 1 and ID 2 for event 2 and so on. I think I want to implement a script or something that will display this event. – Unkown Kid Jul 13 '22 at 08:56
  • You will have to come up with a more concrete requirement. This is what I can help you with so far for the first 50 days. https://onlinephp.io/c/07920 – nice_dev Jul 13 '22 at 09:23
  • I written js code using momentjs. Laravel can use it too. You can see it at here http://jsfiddle.net/tn438zkd/12/ – Mai Truong Jul 13 '22 at 09:26
  • Thank you so much for the solution nice_dev and Mai Truong. This is very helpful. – Unkown Kid Jul 13 '22 at 10:07

2 Answers2

2

With laravel, you can use Carbon

You can refer code bellow

$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
Mai Truong
  • 471
  • 2
  • 8
-1

do something like below.

$day = date("D");  
   switch($day){
     case 'Monday':
          return 'event 1';
     break;
     
     case 'Tuesday':
          return 'event 2';
     break;
      *********
      *********
     case 'Friday':
          return 'event 5';
     break;
     }
Shahanaz B
  • 26
  • 3
  • This is incorrect. Event 1 need not always be on Monday. – nice_dev Jul 13 '22 at 08:37
  • Hi Shahanaz thanks :) , but if you look at my question on Day 11 which is Thursday it will repeat back to Event 1 which mean if I do it in your way Thursday would just return Event 4. Day 12 will be Event 2, Day 13 Event 3 and so on – Unkown Kid Jul 13 '22 at 08:46