Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ready4it's avatar

Carbon - I need to adjust date to a certain interval

Hey!

The user has the option to select : The [Second](value is between 1 and 4) [Monday](value is between 1 and 7) of every [6](value is between 1 and 12)th month

<input type="radio" id="radiobutton16" name="timing" value="every_specific_weekday_of_month"> 
De  
<select onchange="checkOption('radiobutton16')" name="weekdayAmountSelect">
                             <option value="1">Eerste</option>
                             <option value="2">Tweede</option>
                             <option value="3">Derde</option>
                              <option value="4">Vierde</option>
</select>
<select onchange="checkOption('radiobutton16')" name="weekdaySelect">
                              <option value="1">Maandag</option>
                              <option value="2">Dinsdag</option>
                              <option value="3">Woensdag</option>
                              <option value="4">Donderdag</option>
                              <option value="5">Vrijdag</option>
                              <option value="6">Zaterdag</option>
                              <option value="7">Zondag</option>
</select>
van elke
 <input onchange="checkOption('radiobutton16')" id="textbox-terugkeerpatroon" type="number" min="1" max="12"value="1" name="amount_of_months_option_2"style="width:40px">
 maanden<br>

I get all the values in my controller, but I am unsure how to make a date that reflects this jump in time

                $monthly->days = $eerste;  // first second third or fourth day of week in the interval
                $monthly->weekday = $dagvanweek;    //  day of the week
                $monthly->amount_of_months = $iederaantalmaanden;    // month interval

$now = Carbon::Now();
                $now = $recurring->next_trigger;

                    $nextdate = Carbon::parse($now)->hour(8)->minute(0)->second(0);
                    $nextdate->addMonths($request->iederaantalmaanden);

                    // $nextdate->day($request->day_of_month)->hour(8)->minute(0)->second(0);

                $recurring->next_trigger = $nextdate;

I've read somewhere that PHP DateTime has the ability to use the 'first monday in february' thing to parse this, but I am unsure how to end up with the correct date in the $nextdate variable

0 likes
2 replies
lostdreamer_nl's avatar
Level 53

Something like this could work:

$numbers = [
        1 => 'first',
        2 => 'second',
        3 => 'third',
        4 => 'fourth'
];
$days = [
        1 => 'monday',
        2 => 'tuesday',
        3 => 'wednesday',
        4 => 'thursday',
        5 => 'friday',
        6 => 'saturday',
        7 => 'sunday',
];
$months = [
        1 => 'January',
        2 => 'February',
        3 => 'March',
        4 => 'April',
        5 => 'May',
        6 => 'June',
        7 => 'July',
        8 => 'August',
        9 => 'September',
        10 => 'October',
        11 => 'November',
        12 => 'December'
    ];

$dateString = $numbers[$weekdayAmountSelect] .' '. $days[$weekdaySelect] .' of '. $months[$amount_of_months_option_2];
$date = \Carbon\Carbon::parse($dateString);
dd($date->toDayDateTimeString());

Probably best to change your inputs a bit so that you dont have to do the reformatting of 1 => monday, etc. just have the values of the selectbox have 'first / second / third' etc. and the actual english day/month names and pass it on to Carbon::parse();

1 like

Please or to participate in this conversation.