Jump to content

Issue in count date from date range


newphpcoder

Recommended Posts

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

 

Link to comment
Share on other sites

<?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.
}
?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.