PHP code example of rovangju / carbon-nbd

1. Go to this page and download the library: Download rovangju/carbon-nbd library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rovangju / carbon-nbd example snippets


use CarbonExt\NBD\Calculator;
use CarbonExt\NBD\CoreCallbacks C;
use Carbon\Carbon;

$nbd = new Calculator();

/* Date to find the next business day for */
$dt = new Carbon('2014-01-01');

/* Cherry pick special dates */
$nbd->addExclusion(new Carbon('2014-01-02'));
var_dump($nbd->nbd($dt)); /* Carbon obj: 2014-01-03 */



/* Custom exclusion callbacks for layering in complexity */
$nbd->addCallback(C::noWeekends());
$nbd->addCallback(C::ignoreDaysOfWeek(array(4))); /* No Fridays, uses Carbon's 0-based offsets */

/* All callback functions must accept a Carbon object and return a bool value */
$cfn = function(Carbon $dt) {
	return ($dt->day % 2 == 0);
}

$nbd->addCallback($cfn); /* Only on even days of the month */

$nbd->setDeadline(new Carbon('3:00pm'));

$nbd->nbd(new Carbon('2014-01-01 2:59:59pm')); /* Carbon obj: 2014-01-01 00:00:00 */
$nbd->nbd(new Carbon('2014-01-01 3:00:01pm')); /* Carbon obj: 2014-01-02 00:00:00 */

use CarbonExt\NBD\Calculator;
use CarbonExt\NBD\CoreCallbacks as C;
use Carbon\Carbon;

class BusinessDayCalc extends Calculator {

	protected $observedHolidays = array(
		'January 1st', /* New years */
		'July 4th', /* Independence Day */
		'November 28th', /* Thanksgiving */
		'December 25th', /* Christmas */
	);

	public function __construct() {

		$this->addCallback(C::noWeekends());

        $observed = array();
        
        /* Use ignoreRecurring strategy to ignore recurring month-day combos */
		foreach ($this->observedHolidays as $dt) {
			$observed[] = new Carbon($dt);
		}
		
		$this->addCallback(C::ignoreRecurring($observed));
		
		/* Use ignoreNDOW strategy to ignore complex, verbal-oriented exceptions */
		/* Ignore Memorial Day: Last Monday of May */
		$this->addCallback(C::ignoreNDOW(5, -1, 1));
		
		/* Ignore Labor Day: First Monday of September */
		$this->addCallback(C::ignoreNDOW(9, 1, 1));
	}
}

$nbd = new BusinessDayCalc();
/* Now all your business use-case rules are automatically built in */