note 102372 modified in function.strtotime by danbrown

[email protected] Thu, 10 Feb 2011 10:00:33 -0800
Newsgroups php.notes
Message-ID <[email protected]>
Function to add days (excluding specified days/dates)

<?php
function addDays($timestamp, $days, $skipdays=NULL, $skipdates=NULL){
	// $skipdays: array (Monday-Sunday) eg. array("Saturday","Sunday")
	// $skipdates: array (YYYY-mm-dd) eg. array("2012-05-02","2015-08-01");
	$i = 1;
	
	while($days >= $i){
		$timestamp = strtotime("+1 day" ,$timestamp);
		if(in_array(date("l",$timestamp), $skipdays) || in_array(date("Y-m-d",$timestamp), $skipdates)){
			$days++;
		}
		$i++;
	}
	
	return $timestamp;
}
?>

I use this in combination with the calculateBankHolidays function in the notes here: http://php.net/manual/en/ref.calendar.php to skip working days in the UK:

<?php
$skipdays = array("Saturday","Sunday");
$skipdates = array_merge(calculateBankHolidays(date("Y")), calculateBankHolidays(date("Y", strtotime("+1 year"))));

$ts = addDays(strtotime("now"), 1, $skipdays, $skipdates);
?>

--was--
Function to add days (excluding specified days/dates)

function addDays($timestamp, $days, $skipdays=NULL, $skipdates=NULL){
	// $skipdays: array (Monday-Sunday) eg. array("Saturday","Sunday")
	// $skipdates: array (YYYY-mm-dd) eg. array("2012-05-02","2015-08-01");
	$i = 1;
	
	while($days >= $i){
		$timestamp = strtotime("+1 day" ,$timestamp);
		if(in_array(date("l",$timestamp), $skipdays) || in_array(date("Y-m-d",$timestamp), $skipdates)){
			$days++;
		}
		$i++;
	}
	
	return $timestamp;
}

I use this in combination with the calculateBankHolidays function in the notes here: http://php.net/manual/en/ref.calendar.php to skip working days in the UK:

$skipdays = array("Saturday","Sunday");
$skipdates = array_merge(calculateBankHolidays(date("Y")), calculateBankHolidays(date("Y", strtotime("+1 year"))));

$ts = addDays(strtotime("now"), 1, $skipdays, $skipdates);

http://php.net/manual/en/function.strtotime.php