ds00424
ds00424

Reputation: 1348

PHP - Next biweekly meeting date

I am looking to calc in PHP the next bi-weekly (every 2 weeks) meeting from a given date.

So given the first meeting was on Dec 1, 2015 and occurs every 2 weeks, what is the date of the next meeting from a given date?

I think the obvious answer is:

$start = new DateTime('2015-12-01');                        // Meeting origination date
$target = new DateTime('2016-03-10');                       // The given date
$targetPlus = clone $target;                                // Could use DateTimeImmutable for $target (PHP>=5.5)
$targetPlus->modify("+3 weeks");                            // Get a date *past* the next possible meeting                                                          
$interval = new DateInterval("P2W");                        // Create a 2 week interval
$period   = new DatePeriod($start, $interval, $targetPlus); // Get all dates from orig to target+2weeks
foreach ($period as $date) {                                // Look at all dates in the Period
    if ($date > $target) {                                  // find first > target
        print("Next meeting is: " . $date->format('D, d M Y') . "\n");
        break;
    }
}

But wondering if there is a way without looping thru all possible meetings up to the given date (and then some).

Upvotes: 2

Views: 1379

Answers (2)

Dan Abrey
Dan Abrey

Reputation: 696

Try working out the difference between the dates, finding the next multiple of the amount of days between events (in your case 14), and adding that number of days to the start date, like so:

<?php
$daysBetween = 14;
$start = new DateTime('2015-12-01');                        // Meeting origination date
$target = new DateTime('2016-03-10');                       // The given date
$daysApart = $start->diff($target)->days;                   
$nextMultipleOfDaysBetweenAfterDaysApart = ceil($daysApart/$daysBetween) * $daysBetween;
$dateOfNextMeeting = $start->modify('+' . $nextMultipleOfDaysBetweenAfterDaysApart . 'days');
var_dump($dateOfNextMeeting);
?>

Upvotes: 0

alexander.polomodov
alexander.polomodov

Reputation: 5534

Yes, there are more clear approach, you can just calculate date of new meeting $targetMeeting with this code:

<?php
date_default_timezone_set('America/Los_Angeles');

$start = new DateTime('2015-12-01');                        // Meeting origination date
$target = new DateTime('2016-03-10');                       // The given date
$targetPlus = clone $target;                                // Could use DateTimeImmutable for $target (PHP>=5.5)
$targetPlus->modify("+3 weeks");                            // Get a date *past* the next possible meeting
$interval = new DateInterval("P2W");                        // Create a 2 week interval

/* -- Begin of new code --*/
$targetMeeting = clone $target;
$intervalBetweenTargetAndStart = $target->diff($start);
$daysBeforeMeeting = 14 - $intervalBetweenTargetAndStart->days % 14;
$targetMeeting->modify("+".$daysBeforeMeeting." days");
print("Next meeting is: " . $targetMeeting->format('D, d M Y') . "\n");
/* -- End of new code --*/

$period   = new DatePeriod($start, $interval, $targetPlus); // Get all dates from orig to target+2weeks
foreach ($period as $date) {                                // Look at all dates in the Period
    if ($date > $target) {                                  // find first > target
        print("Next meeting is: " . $date->format('D, d M Y') . "\n");
        break;
    }
}

Output:

Next meeting is: Tue, 22 Mar 2016
Next meeting is: Tue, 22 Mar 2016

Upvotes: 1

Related Questions