Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Monthly Recurrence

I'm Using PHP RRule for Recurrence in my Project. Here Daily and Weekly Recurrence are working fine. I don't know how to use monthly Recurrence in my scenario.

Monthly scenario:

First Sunday of every 2 Months

enter image description here

Code:

$dow=array("SU");
$rrule = new RRule([
    'FREQ' => 'MONTHLY',
    'BYMONTHDAY' => 2,  
    'INTERVAL' => 2,
    'BYDAY' => $dow,    
    'DTSTART' => '2016-12-01',
    'UNTIL' => '2017-12-31'
]); 

foreach ( $rrule as $occurrence ) {
    echo "</br>";
    echo $occurrence->format('D Y-m-d');
}

echo $rrule->humanReadable(),"\n";

//OUTPUT
Sun 2017-04-02
every 2 months on Sunday on the 2nd of the month, starting from 12/1/16, until 12/31/17 
like image 466
Maria Jeysingh Anbu Avatar asked May 31 '26 08:05

Maria Jeysingh Anbu


1 Answers

You don't need BYMONTHDAY, you need BYDAY with a number prefix.

In your situation: 'BYDAY' => '1SU' means "the first Sunday". 2SU the second Sunday, -1SU the last Sunday etc.

More information:

  • https://github.com/rlanvin/php-rrule/issues/19
  • https://www.rfc-editor.org/rfc/rfc5545#section-3.3.10
like image 140
rlanvin Avatar answered Jun 02 '26 22:06

rlanvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!