note 102372 added to function.strtotime

[email protected] Thu, 10 Feb 2011 09:36:17 -0800
Newsgroups php.notes
Message-ID <[email protected]>
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);
----
Server IP: 92.48.74.199
Probable Submitter: 82.33.205.163
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit        -- https://master.php.net/note/edit/102372
Del: integrated  -- https://master.php.net/note/delete/102372/integrated
Del: useless     -- https://master.php.net/note/delete/102372/useless
Del: bad code    -- https://master.php.net/note/delete/102372/bad+code
Del: spam        -- https://master.php.net/note/delete/102372/spam
Del: non-english -- https://master.php.net/note/delete/102372/non-english
Del: in docs     -- https://master.php.net/note/delete/102372/in+docs
Del: other reasons-- https://master.php.net/note/delete/102372
Reject      -- https://master.php.net/note/reject/102372
Search      -- https://master.php.net/manage/user-notes.php