note 102372 modified in function.strtotime by danbrown

From: Date: Thu, 10 Feb 2011 18:00:33 +0000
Subject: note 102372 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176874@lists.php.net to get a copy of this message
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

« previous php.notes (#176874) next »