Jump to content

Array sum value issue


gmc1103

Recommended Posts

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.