note 118136 added to function.date-diff

From: Date: Tue, 13 Oct 2015 00:11:32 +0000
Subject: note 118136 added to function.date-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-203940@lists.php.net to get a copy of this message
My simple function to count up the number of weekdays between the dates (inclusive for both ends), one can add a holidays between the dates to take them into account as well as the weekends, the weekend days could also be changed. function count_week_days($__date_from, $__date_to, $__holidays_between=array(), $__weekend_days=array(5,6)) { $total_days_count = $__date_to > $__date_from ? round(($__date_to - $__date_from)/(24*3600)) : 0; $full_weeks_count = floor($total_days_count/7); $weekend_days_count = $full_weeks_count*count($__weekend_days); $days_left_uncovered = $total_days_count - $full_weeks_count*7; for($i = 0; $i < $days_left_uncovered; $i++) { $date_to_check = $i ? strtotime("+{$i} day", $__date_from) : $__date_from; if(in_array(date('N', $date_to_check), $__weekend_days)) { $weekend_days_count++; } } $week_days_count = $total_days_count - $weekend_days_count - count($__holidays_between); return $week_days_count; } Tests: print "\n 12.10.2015 to 10.10.2015 diff=".count_week_days(strtotime('12.10.2015'), strtotime('10.10.2015')); print "\n 12.10.2015 to 13.10.2015 diff=".count_week_days(strtotime('12.10.2015'), strtotime('13.10.2015')); print "\n 12.10.2015 to 15.10.2015 diff=".count_week_days(strtotime('12.10.2015'), strtotime('26.10.2015')); print "\n 12.10.2015 to 13.10.2015 diff=".count_week_days(strtotime('12.10.2015'), strtotime('13.10.2016')); print "\n 12.10.2015 to 13.10.2015 diff=".count_week_days(strtotime('12.10.2015'), strtotime('12.11.2015')); print "\n 12.10.2015 to 13.10.2015 diff=".count_week_days(strtotime('12.10.2015'), strtotime('12.11.2015'), array(strtotime('07.11.2015'))); Results: 12.10.2015 to 10.10.2015 diff=0 12.10.2015 to 13.10.2015 diff=1 12.10.2015 to 15.10.2015 diff=10 12.10.2015 to 13.10.2015 diff=263 12.10.2015 to 13.10.2015 diff=23 12.10.2015 to 13.10.2015 diff=22 ---- Server IP: 72.52.91.14 Probable Submitter: 5.228.218.231 ---- Manual Page -- http://php.net/manual/en/function.date-diff.php Edit -- https://master.php.net/note/edit/118136 Del: integrated -- https://master.php.net/note/delete/118136/integrated Del: useless -- https://master.php.net/note/delete/118136/useless Del: bad code -- https://master.php.net/note/delete/118136/bad+code Del: spam -- https://master.php.net/note/delete/118136/spam Del: non-english -- https://master.php.net/note/delete/118136/non-english Del: in docs -- https://master.php.net/note/delete/118136/in+docs Del: other reasons-- https://master.php.net/note/delete/118136 Reject -- https://master.php.net/note/reject/118136 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203940) next »