Search code examples
phplaravelcalendarphp-carbon

How to set alternate week days off in laravel using carbon package?


I've made a custom calendar and I want to set the alternate weekdays off like the image given bellow how can we set that in larvel using carbon package? enter image description here


Solution

  • $firstDate = Carbon::parse('2022-09-07');
    $limit = '2022-12-31';
    
    // List dates:
    foreach ($firstDate->range($limit, '2 weeks') as $date) {
      echo $date; // 2022-09-07, 2022-09-21, 2022-10-05, ...
    }
    
    // Check if days is a day off:
    function isDayOff($date) {
      return ((int) Carbon::parse('2022-09-07')->diffInDays($date)) % 14 === 0;
    }
    
    if (isDayOff('2022-10-19')) {
      echo "Yes 2022-10-19 is off";
    }