Ok I have a function that counts the number of working hours between two dates and returns the time in minuets. Working Hours are between 08:30 and 17:30 excluding weekends.
This seems to be working fine on my development machine but when I put it on the live server for some reason the settime method jumps forward one hour in the middle of a loop.
Here is the code
$onHoldStart = '2014-10-07 17:09:00';
$onHoldEnd = '2014-10-08 14:36:18';
$onHoldHours = biss_hours($onHoldStart, $onHoldEnd);
echo $onHoldHours.'<br />';
echo date('Y-m-d H:i:s').'<br />';
system('date +%Z');
/**
* Counts the number of working hours between two given dates
* E.g. Hours between 08:30 and 17:30 excluding weekends.
* Takes start date and end date (Y-m-d H:i:s)
*
*/
function biss_hours($start, $end){
$logfile='customlog.log';
$log='';
$log .='raw start '.$start."
";
$startDate = new DateTime($start);
$log .= 'start '. $startDate->format('Y-m-d H:i:s')."
";
//date time period starts counting from the same hour so add hour to start to compensate
$startDate->add(new DateInterval('PT1H'));
$endDate = new DateTime($end);
$periodInterval = new DateInterval( "PT1H" );
$period = new DatePeriod( $startDate, $periodInterval, $endDate );
$count = 0;
foreach($period as $date){
$log .= 'DATE '. $date->format('Y-m-d H:i:s')."
";
$startofday = clone $date;
$startofday->setTime(8,30);
$log .= 'start of day '. $startofday->format('Y-m-d H:i:s')."
";
$endofday = clone $date;
$endofday->setTime(17,30);
if($date > $startofday && $date <= $endofday && !in_array($date->format('l'), array('Sunday','Saturday'))){
$count++;
}
}
$log .='count '.$count;
$test = file_put_contents($logfile, $log);
return ($count * 60) + biss_minutes($start, $end);
}
function biss_minutes($start, $end){
$diff = strtotime($end) - strtotime($start);
$in = intval(date('i',$diff));
return $in;
}
As you can see this code also adds a log file, the log file shows the discrepancy I am talking about.
Log file from my local machine:
raw start 2014-10-07 17:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 19:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 20:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 21:09:00
(Skip a few)
count 6
Log file from the server:
raw start 2014-10-07 17:09:00
start 2014-10-07 17:09:00
DATE 2014-10-07 18:09:00
start of day 2014-10-07 08:30:00
DATE 2014-10-07 19:09:00
start of day 2014-10-07 09:30:00
DATE 2014-10-07 20:09:00
start of day 2014-10-07 09:30:00
DATE 2014-10-07 21:09:00
start of day 2014-10-07 09:30:00
DATE 2014-10-07 22:09:00
start of day 2014-10-07 09:30:00
DATE 2014-10-07 23:09:00
start of day 2014-10-07 09:30:00
DATE 2014-10-08 00:09:00
start of day 2014-10-08 09:30:00
DATE 2014-10-08 01:09:00
start of day 2014-10-08 09:30:00
count 5
As you can see the 08:30:00 has suddenly became 09:30:00 after the first iteration of the loop. Why is this happening? Both servers are set to BST the current uk timezone.
The correct output should be
387 2014-10-08 16:32:47 BST
But because of this hour jump the server output is :
327 2014-10-08 16:33:16 BST
Removing 60 minuets.
Anybody got any ideas?