newphpcoder Posted March 7, 2012 Share Posted March 7, 2012 Hi... I encountered problem in my counting number of dates from date to date. for example : he file leave: · Feb. 18, Saturday · Feb. 20, Monday · Feb. 21, Tuesday $DATE_LEAVE_FROM = 2012-02-18 $DATE_LEAVE_TO = 2012-02-21 Using this code: <?php $DATE_LEAVE_FROM = $_GET['DATE_LEAVE_FROM']; $DATE_LEAVE_TO = $_GET['DATE_LEAVE_TO']; $DATE_FROM = strtotime($DATE_LEAVE_FROM, 0); $DATE_TO = strtotime($DATE_LEAVE_TO, 0); $difference = ($DATE_TO - $DATE_FROM); $HOURS_LEAVE = floor($difference / 86400); ?> usiing this code the output is 4, because of the from date to date range which is correct. BUt I need to count only the date which is from Monday to Saturday or should I say don't count date which is Sunday.. Is it possible?How? Thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/258425-issue-in-count-date-from-date-range/ Share on other sites More sharing options...
jcbones Posted March 7, 2012 Share Posted March 7, 2012 That is not the HOURS of leave, that is the DAYS of leave. Which do you want? I have an old function in my library, that I could make work pretty quickly (I think). Quote Link to comment https://forums.phpfreaks.com/topic/258425-issue-in-count-date-from-date-range/#findComment-1324706 Share on other sites More sharing options...
jcbones Posted March 7, 2012 Share Posted March 7, 2012 <?php $leaveStarts = '2012-03-08'; //starting date; $leaveEnds = '2012-03-13';//ending date; echo 'You have requested ' . hoursOfLeave($leaveStarts,$leaveEnds,7,15,array('Sunday')) . ' hours of leave.'; //function usage, printing to screen. function hoursOfLeave($timeStarts,$timeEnds,$workStarts,$workEnds,$skipDays = array()) { //NOTE: workstarts, and workEnds is 24 hour clock. $hoursPerDay = $workEnds - $workStarts; //how many hours per day does work last. $hours = 0; //start our hour count. $start = strtotime($timeStarts); //cast the start time to a unix timestamp. $end = strtotime($timeEnds); //same for the end time. for($i = $start; $i < $end; $i += 86400) { //set i to the starting timestamp, continue to loop until i is greater than or equal to the end timestamp, adding a full days seconds to i on every loop. if(!empty($skipDays) && in_array(date('l',$i),$skipDays)) { //if there are skip days, and the current timestamp(i) says we are inside of it. continue; //start a new loop cycle (bypass counting the hours of this day). } $hours += $hoursPerDay; //add the hoursPerDay to our hour count. } return $hours; //after we are completely done counting, just return the hours from the function. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258425-issue-in-count-date-from-date-range/#findComment-1324719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.