gmc1103 Posted July 10, 2020 Share Posted July 10, 2020 Hello I'm trying to get this working but i'm stuck I'm making a program who must define dates and time (hours) to a task So in the beginning i define: how many hours the task will have the week days the max hour per day The following code is working well except if the total is 20, and i have the sum of 18, the script allows to add more than 2 hours before stop the execution Example running the script hours defined: 20 Data: 2020-09-01 -Hours:4 Data: 2020-09-07 -Hours:2 Data: 2020-09-08 -Hours:4 Data: 2020-09-14 -Hours:2 Data: 2020-09-15 -Hours:4 Data: 2020-09-21 -Hours:2 Data: 2020-09-22 -Hours:4 Total: 22 This is my code ( the problem should be inside foreach range code) define('INTERNAL_FORMAT', 'Y-m-d'); $startDate = "2020-09-01"; $start_date1 = strtotime("2020-09-01"); $endDate = strtotime("2021-06-30"); $horas = "20"; $sum = 0; $datediff = round(($endDate - $start_date1) / (60 * 60 * 24)) ; $res = array('01-01','02-25','04-10','04-12','04-25','05-01','06-10', '06-11', '08-15', '10-05', '11-01', '12-01', '12-08', '12-25'); $period = new DatePeriod( new DateTime('2021-03-27'), new DateInterval('P1D'), new DateTime('2021-04-16') ); $arr = array(); foreach ($period as $key => $value) { $arr[] = $value->format('m-d'); } $excluded_dates = array_merge($res, $arr); function isSegunda($date) { return date('w', strtotime($date)) === '1'; } function isTerca($date) { return date('w', strtotime($date)) === '2'; } function isQuarta($date) { return date('w', strtotime($date)) === '3'; } function isQuinta($date) { return date('w', strtotime($date)) === '4'; } function isSexta($date) { return date('w', strtotime($date)) === '5'; } // handle the excluded dates function isExcludedDate($internal_date) { global $excluded_dates; $str2 = substr($internal_date, 5); foreach($excluded_dates as $row){ return in_array($str2, $excluded_dates); } } // something to store months and days $cronograma = array(); $isSegunda = "1"; $isTerca = "2"; $isQuarta = ""; $isQuinta = ""; $isSexta = ""; $temposSegunda = "2"; $temposTerca = "4"; $temposQuarta = "4"; $temposQuinta = "4"; $temposSexta = "4"; foreach(range(0,$datediff) as $day) { $internal_date = date(INTERNAL_FORMAT, strtotime("{$startDate} + {$day} days")); $this_day = date(INTERNAL_FORMAT, strtotime($internal_date)); $this_month = date(INTERNAL_FORMAT, strtotime($internal_date)); if($isSegunda != null && $sum<$horas){ if ((isSegunda($internal_date)) && !isExcludedDate($internal_date)) { $cronograma[$this_month][] = $this_day; if($temposSegunda != null){ $cronograma[$this_month][] = $temposSegunda; } $sum += $temposSegunda; } } if($isTerca != null && $sum<$horas){ if ((isTerca($internal_date)) && !isExcludedDate($internal_date)) { $cronograma[$this_month][] = $this_day; if($temposTerca != null){ $cronograma[$this_month][] = $temposTerca; } $sum += $temposTerca; } } if($isQuarta != null){ if ((isQuarta($internal_date)) && !isExcludedDate($internal_date)) { $cronograma[$this_month][] = $this_day; if($temposQuarta != null){ $cronograma[$this_month][] = $temposQuarta; } } } if($isQuinta != null){ if ((isQuinta($internal_date)) && !isExcludedDate($internal_date)) { $cronograma[$this_month][] = $this_day; if($temposQuinta != null){ $cronograma[$this_month][] = $temposQuinta; } } } if($isSexta !=null){ if ((isSexta($internal_date)) && !isExcludedDate($internal_date)) { $cronograma[$this_month][] = $this_day; if($temposSexta != null){ $cronograma[$this_month][] = $temposSexta; } } } } $soma = 0; echo "hours defined: ".$horas; print "<br>"; foreach($cronograma as $month => $days) { $dias = $days[0]; $tempos = $days[1]; echo "Data: ".$dias." -Hours:".$tempos; print "<br>"; $soma+= $days[1]; } echo "Total: ".$soma; Any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311059-array-sum-value-issue/ Share on other sites More sharing options...
requinix Posted July 10, 2020 Share Posted July 10, 2020 Think about how the code is working. "If the sum is less than the hour limit, add more to the sum." That will keep adding and adding until you go over the limit. You have to stop before you go over the limit. Quote Link to comment https://forums.phpfreaks.com/topic/311059-array-sum-value-issue/#findComment-1579486 Share on other sites More sharing options...
gmc1103 Posted July 10, 2020 Author Share Posted July 10, 2020 Hi Thanks for reading and replying But it stops, you can see he has stopped when $sum > $hours what i expect is to get the same value in variable $hours, and in my array. For example i choose monday and i can work 6 hours, Task is 20 hours, so i need 6+6+6+2, but when the user define 6 hours for each monday i will have 6+6+6+6, total is 24. I was thinking in create a statement inside if($isSegunda != null && $sum<$horas){ if ((isSegunda($internal_date)) && !isExcludedDate($internal_date)) { $cronograma[$this_month][] = $this_day; if($temposSegunda != null){ $cronograma[$this_month][] = $temposSegunda; } $sum += $temposSegunda; } So before adding the $temposSegunda into the array, i check the total sum and the task hours, and if i get more hours, i remove the number in excess, in this case i have task hours = 20 and the total is 24, the difference is 4, so how can alter this to write the correct value into the array? Any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311059-array-sum-value-issue/#findComment-1579490 Share on other sites More sharing options...
requinix Posted July 10, 2020 Share Posted July 10, 2020 Honestly I don't quite follow exactly what's going on, but if all you need to do is cut off the hours at 20 then I'd think you should simply check the updated $sum and do a bit of math if it's above $horas. Quote Link to comment https://forums.phpfreaks.com/topic/311059-array-sum-value-issue/#findComment-1579492 Share on other sites More sharing options...
Barand Posted July 10, 2020 Share Posted July 10, 2020 $hours_per_day = 6; $total_hours = 20; $days = floor($total_hours / $hours_per_day); $hours_remaining = $total_hours - $days * $hours_per_day; echo "$days days and $hours_remaining hours" ; //--> 3 days and 2 hours Quote Link to comment https://forums.phpfreaks.com/topic/311059-array-sum-value-issue/#findComment-1579498 Share on other sites More sharing options...
gmc1103 Posted July 11, 2020 Author Share Posted July 11, 2020 17 hours ago, Barand said: $hours_per_day = 6; $total_hours = 20; $days = floor($total_hours / $hours_per_day); $hours_remaining = $total_hours - $days * $hours_per_day; echo "$days days and $hours_remaining hours" ; Hi Barand...you are the boss. Great solution, it works like a charm. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/311059-array-sum-value-issue/#findComment-1579511 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.