Support the ongoing development of Laravel.io →
Views Blade Validation
Last updated 2 years ago.
0

I don't think Carbon has what your asking for.

I set up a test route and this is my result for what your asking for.


Route::get('/testcarbon', function() {

    // a couple of holidays as timestamps
    $holidays[] = strtotime("01/23/15");
    $holidays[] = strtotime("01/28/15");

    var_dump($holidays);

    // test date
    $date = "01/21/15";

    // convert to carbon
    $MyDateCarbon = \Carbon\Carbon::parse($date);
    echo "entered date : " . $MyDateCarbon->timestamp . ' - ' . $MyDateCarbon->toRfc2822String(). '<BR>';

    // add three days to selected date
    $MyDateCarbon->addDays(3);
    echo "query date : " .  $MyDateCarbon->timestamp . ' - ' .($MyDateCarbon->toRfc2822String()). '<BR>';

    while (true) {

        // verify date,  not in holiday and is not weekend
        if (in_array($MyDateCarbon->timestamp, $holidays) || $MyDateCarbon->isWeekend()) {

            echo "-- is holiday or weekend ". $MyDateCarbon->toRfc2822String() . " <BR>";

            // the day is either in the holidays array or is a weekend
            // add one day
            $MyDateCarbon->addDay();
        } else {
            // ok, day should be good, exit while
            break;
        }

    }

    echo "final date: " . $MyDateCarbon->toRfc2822String(). '<BR>';

    echo "<hr>done";

});

Hope that helps

0
Solution selected by @driesvints

Hi TerrePorter,

Thanks for your help! There is a little problem with the code though, it doesn't take into account if the second or third day is a Weekend or Holiday. So I changed it up a little.

$holidays = ["2015-01-01", "2015-01-02"];

$date = "12/31/14";
$MyDateCarbon = Carbon::parse($date);

$MyDateCarbon->addWeekdays(3);

for ($i = 1; $i <= 3; $i++;) {

    if (in_array(Carbon::parse($date)->addWeekdays($i)->toDateString(), $holidays)) {

        $MyDateCarbon->addDay();

    }
}

dd($MyDateCarbon);

So basically it adds 3 Weekdays, and check if any of the Weekdays added is a holiday and if there is any, it'll add a day to the final date. Thanks! Couldn't have thought of it without you.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

adrielzxdf adrielzxdf Joined 13 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.