How to get a DatePeriod with “every first thursday in month”?
-
29-05-2021 - |
質問
I'm having two DateTime
-objects:
$start = new DateTime('first thursday of June 2012');
$end = new DateTime('2012-12-31');
I need a DatePeriod
that contains all first Thursdays of the months between this two dates. When using
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
This only adds 1 month without respecting the condition "first thursday".
Also something like this does not work:
$interval = DateInterval::createFromDateString('1 month first thursday');
$period = new DatePeriod($start, $interval, $end);
Does anyone know how I can achieve this?
解決
I've struggled with this the last two hours - as soon as I posted it here I got the solution. I only had to change the DateInterval string to "first thursday of next month".
$start = new DateTime('first thursday of June 2012');
$end = new DateTime('2012-12-31');
$interval = DateInterval::createFromDateString('first thursday of next month');
$period = new DatePeriod($start, $interval, $end);
Works! DateTime rocks ;-)
所属していません StackOverflow