简体   繁体   中英

How to get a DatePeriod with "every first thursday in month"?

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 ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM