John Davidson

php - Calculation downtime in minute start from '0' when the value bigger than 24 hour

0 comments
Message:


I need to calculate downtime in minute based on start date of business hour ,end date of business hour start date and end date.The problem is when my downtime value exceeds 24 hour which in minute is 1440 minutes , downtime value become start from '0' .


Let say my start date is 2021-09-01 09:00:00


$startdateTimes = strtotime('2021-09-01 09:00:00');

End date is 2021-09-03 16:00:00


$enddateTimes = strtotime('2021-09-03 16:00:00'); 

Start time Business Hour is 8:30


$bhStartTime = '8:30';

End time Business Hour 17:30


$bhEndTime = '17:30'

My full code


$startdateTimes = strtotime('2021-09-01 09:00:00'); //start date

$setdate12AM = new DateTime(date('Y-m-d',$startdateTimes));
$setdate12AM->setTime('0','0');
$setdate12AM->format('Y-m-d H:i:s');
$date12AM = $setdate12AM->getTimestamp();

$bhStartTime = '8:30'; //biz hour start time
$bhStartSplit = explode(":",$bhStartTime);
$setbhStartTimesDate = new DateTime(date('Y-m-d',$startdateTimes));
$setbhStartTimesDate->setTime($bhStartSplit[0], $bhStartSplit[1]);
$setbhStartTimesDate->format('Y-m-d H:i:s');
$bhStartTimes = $setbhStartTimesDate->getTimestamp();

$bhEndTime = '17:30' //biz hour end time
$bhEndSplit = explode(":",$bhEndTime);
$setbhEndTimesDate = new DateTime(date('Y-m-d',$startdateTimes));
$setbhEndTimesDate->setTime($bhEndSplit[0], $bhEndSplit[1]);
$setbhEndTimesDate->format('Y-m-d H:i:s');


if(strtotime('12:00am') < strtotime($bhEndTime) && strtotime($bhStartTime) > strtotime($bhEndTime)){
$bhEndTimes = $setbhEndTimesDate->getTimestamp();
$bhEndTimes = strtotime("+1 day", $bhEndTimes);
}else{
$bhEndTimes = $setbhEndTimesDate->getTimestamp();
}
//----------------------------------------------

$enddateTimes = strtotime('2021-09-03 16:00:00'); //end date


$setdate12AMEnd = new DateTime(date('Y-m-d',$enddateTimes));
$setdate12AMEnd->setTime('0','0');
$setdate12AMEnd->format('Y-m-d H:i:s');
$date12AMEnd = $setdate12AMEnd->getTimestamp();

$bhStartSplitEnd = explode(":",$bhStartTime); //biz hour start time
$setbhStartTimesDateEnd = new DateTime(date('Y-m-d',$enddateTimes));
$setbhStartTimesDateEnd->setTime($bhStartSplitEnd[0], $bhStartSplitEnd[1]);
$setbhStartTimesDateEnd->format('Y-m-d H:i:s');
$bhStartTimesEnd = $setbhStartTimesDateEnd->getTimestamp();

$bhEndSplitEnd = explode(":",$bhEndTime); //biz hour end time
$setbhEndTimesDateEnd = new DateTime(date('Y-m-d',$enddateTimes));
$setbhEndTimesDateEnd->setTime($bhEndSplitEnd[0], $bhEndSplitEnd[1]);
$setbhEndTimesDateEnd->format('Y-m-d H:i:s');

if(strtotime('12:00am') < strtotime($bhEndTime) && strtotime($bhStartTime) > strtotime($bhEndTime)){
$bhEndTimesEnd = $setbhEndTimesDateEnd->getTimestamp();
$bhEndTimesEnd = strtotime("+1 day", $bhEndTimesEnd);
}else{
$bhEndTimesEnd = $setbhEndTimesDateEnd->getTimestamp();
}


if($startdateTimes >= $bhStartTimes && $startdateTimes <= $bhEndTimes){

$ExactStartDate = $startdateTimes; //start at incident start date


}else if($startdateTimes >= $bhEndTimes){
$ExactStartDate = strtotime("+1 day", $bhStartTimes); //add 1 day and start at 8.00am


}else if($startdateTimes <= $bhStartTimes && $startdateTimes >= $date12AM){
$ExactStartDate = $bhStartTimes; //start at 8.00am same date
}

if($enddateTimes >= $bhStartTimesEnd && $enddateTimes <= $bhEndTimesEnd){
$ExactEndDate = $enddateTimes; //end at incident end date


}else if($enddateTimes >= $bhEndTimesEnd){
$ExactEndDate = $bhEndTimesEnd; //end at 6.00pm same date

}else if($enddateTimes <= $bhStartTimesEnd && $enddateTimes >= $date12AMEnd){
$ExactEndDate = $bhStartTimesEnd; //start at 8.00am same date
}


while($ExactStartDate < $ExactEndDate){


$setdailyEndDate = new DateTime(date('Y-m-d',$ExactStartDate));
$setdailyEndDate->setTime($bhEndSplitEnd[0], $bhEndSplitEnd[1]);
$setdailyEndDate->format('Y-m-d H:i:s');
//dd( $setdailyEndDate);
if(strtotime('12:00am') < strtotime($bhEndTime) && strtotime($bhStartTime) > strtotime($bhEndTime)){
$dailyEndDate = $setdailyEndDate->getTimestamp();
$dailyEndDate = strtotime("+1 day", $dailyEndDate);

}else{
$dailyEndDate = $setdailyEndDate->getTimestamp();
//dd( $setdailyEndDate);
}

if($dailyEndDate < $ExactEndDate){
$datediff = $dailyEndDate - $ExactStartDate;
$downTime += $datediff;

}else{
$datediff = $ExactEndDate - $ExactStartDate;
$downTime += $datediff;


}


$setdailyStartDate = new DateTime(date('Y-m-d',$ExactStartDate));
$setdailyStartDate->setTime($bhStartSplitEnd[0], $bhStartSplitEnd[1]);
$setdailyStartDate->format('Y-m-d H:i:s');
$dailyStartDate = $setdailyStartDate->getTimestamp();

$ExactStartDate = strtotime("+1 day", $dailyStartDate);

}
}
}
}
}


$downTime = (gmdate('H', $downTime) * 60) + (gmdate('i', $downTime)); //convert to minute , expected output '1500'

My expected output is 1500 minutes which in hour is 25 hour based on start/end date and start/end business hour....when i change my end date lower than 24 hour...the value is correct but up to 24 hour my value start from '0'



My Answer:
To calculate downtime in minutes, you need to know the start and end times of the downtime. Here is an example code in PHP to calculate downtime in minutes:

php
$start_time = strtotime('2022-01-01 08:00:00'); // Start time of downtime
$end_time = strtotime('2022-01-01 09:30:00'); // End time of downtime

$downtime_minutes = round(($end_time - $start_time) / 60); // Calculate downtime in minutes

echo "Downtime: $downtime_minutes minutes";


In this code, we first convert the start and end times to Unix timestamps using the `strtotime` function. Then, we calculate the downtime in minutes by subtracting the start time from the end time and dividing by 60 (since there are 60 seconds in a minute). Finally, we round the result to the nearest minute and display the downtime in minutes.

Rate this post

4 of 5 based on 1069 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us