How do I get a given function to return the value of a date variable and a numeric one in php?

Asked

Viewed 84 times

-1

I created a function that calculates the next working day of a given date that it receives, but in addition to returning me this new date I wanted it to increment one number more than one counting variable ($prdia) and return me this variable as well. How do I do that?

The code is as follows::

  if ($tipo_prz == 'DU'){
    $prdia = 1;    
  //a variável 'prz' foi obtida em outra parte do código   
  // a data_corrigida_DU também foi obtida em outra parte    
      while ($prdia < $prz){
        $data_corrigida_DU = proximoDiaUtilPr($data_corrigida_DU);
      }
    }

The function is like this:

function proximoDiaUtilPr($data, $saida = 'Y-m-d') {
// Converte $data em um UNIX TIMESTAMP
$timestamp = strtotime($data);
// Calcula qual o dia da semana de $data
// O resultado será um valor numérico:
// 1 -> Segunda ... 7 -> Domingo
$dia = date('N', $timestamp);

// Se for sábado (6) ou domingo (7), calcula a próxima segunda-feira
if ($dia == 6) {
    $timestamp = strtotime("+2 days",strtotime($data));
    $timestamp_final = $timestamp;
    //$timestamp_final = $timestamp + ((8 - $dia) * 3600 * 24);
}

elseif ($dia == 7) {
    $timestamp = strtotime("+1 days",strtotime($data));
    $timestamp_final = $timestamp;
    //$timestamp_final = $timestamp + ((8 - $dia) * 3600 * 24);
}

else {
    // Não é sábado nem domingo acrescenta um dia e incrementa prdia
    $timestamp = strtotime("+1 day",strtotime($data));
    $timestamp_final = $timestamp;
    $prdia++;
}
return date($saida, $timestamp_final);
//echo proximoDiaUtil('2016-09-04');

}


The following error appeared when applying the solution you proposed:

Warning: strtotime() expects Parameter 1 to be string, array Given in C: Users#8188e Desktop sicob_current new_xampp htdocs geq diautil.php on line 41

  • And why do you need this? It doesn’t seem necessary, but it might just be because it lacks context.

  • I need him to do this increment to account for the working days passed during the date increment. For example I have $prz = 30 and $prdia = 1; .

  • It is that your code does not use this, so it is difficult to see this need. Without seeing it seems to me that you do not need, even if you say you need.

  • Correcting my comment above: ... That way every time I call the working day function and the date drops at the weekend I DON’T make the $prdia add-on but when it falls on working day I make an add-on to this variable. I am using this variable only so that when I call the function and it drops on business day the function signal me this bringing the variable $prdia plus a number, understand? What I need is to calculate a term ($prz) on working days. If there is an easier way to do this, it also serves.

  • I’m trying to understand the need to be able to help, because this is conceptually wrong, but it’s hard. When the requirement is confusing it becomes complicated to find the right solution.

  • Got it, buddy. I’m going to try to put it in a better context: My system gets a certain deadline that should be accounted for on working days. I get this term and base date through a query to the database and store each of them in a variable. After that I have to pick up this base date and add 30 working days to it.

Show 1 more comment

2 answers

0

Guys, I was really thinking incorrectly to solve my problem just use the function this way:

Function:

function proximoDiaUtilPr($data, $saida = 'Y-m-d') {
// Converte $data em um UNIX TIMESTAMP
$timestamp = strtotime($data);
// Calcula qual o dia da semana de $data
// O resultado será um valor numérico:
// 1 -> Segunda ... 7 -> Domingo
$dia = date('N', $timestamp);

if ($dia == 5) {
$timestamp = strtotime("+3 days",strtotime($data));
$timestamp_final = $timestamp;
}
// Se for sábado (6) ou domingo (7), calcula a próxima segunda-feira
elseif ($dia == 6) {
    $timestamp = strtotime("+2 days",strtotime($data));
    $timestamp_final = $timestamp;
    //$timestamp_final = $timestamp + ((8 - $dia) * 3600 * 24);
}

else {
    // Não é sábado nem domingo, mantém a data de entrada
    $timestamp = strtotime("+1 day",strtotime($data));
    $timestamp_final = $timestamp;

}
return date($saida, $timestamp_final);
//echo proximoDiaUtil('2016-09-04');

}

Master code:

if ($tipo_prz == 'DU'){
$prdia = 1;
  while ($prdia < $prz){
    $data_corr_DU = proximoDiaUtilPr($data_corr_DU);
    $prdia++;
 }
} 

$text = "".$prz.";".$tipo_prz.";".$data_corr_DU;   
fwrite($arquivoj, $text);
}

0

Why not use an array as a return?

function proximoDiaUtilPr($data, $saida = 'Y-m-d') {
// Converte $data em um UNIX TIMESTAMP
$timestamp = strtotime($data);
// Calcula qual o dia da semana de $data
// O resultado será um valor numérico:
// 1 -> Segunda ... 7 -> Domingo
$dia = date('N', $timestamp);

// Se for sábado (6) ou domingo (7), calcula a próxima segunda-feira
if ($dia == 6) {
    $timestamp = strtotime("+2 days",strtotime($data));
    $timestamp_final = $timestamp;
    //$timestamp_final = $timestamp + ((8 - $dia) * 3600 * 24);
}

elseif ($dia == 7) {
    $timestamp = strtotime("+1 days",strtotime($data));
    $timestamp_final = $timestamp;
    //$timestamp_final = $timestamp + ((8 - $dia) * 3600 * 24);
}

else {
    // Não é sábado nem domingo acrescenta um dia e incrementa prdia
    $timestamp = strtotime("+1 day",strtotime($data));
    $timestamp_final = $timestamp;
    $prdia++;
}
return array(date($saida, $timestamp_final), $prdia);
//echo proximoDiaUtil('2016-09-04');
}

When calling the function, take its return and use the array positions to retrieve the date and $prdia

$foo = proximoDiaUtilPr();
echo $foo[0]; // exibe a data
echo $foo[1]; // exibe o valor de $prdia
  • The following error appeared when applying the solution you proposed: Warning: strtotime() expects Parameter 1 to be string, array Given in C: Users#8188e Desktop sicob_xampp htdocs geq diautil.php on line 4

  • I edited the answer for you to understand better

  • Renato, I tried to implement your solution but the function did not return me the variable $prdia with the addition of 1. I added in the function:code //acrescentei na função:&#xA; return array(date($saida, $timestamp_final), $prdia); and in the code: code $foo = proximoDiaUtilPr($data_corr_DU);&#xA; $data_corr_DU = $foo[0];&#xA; $prdia = $foo[1];&#xA; echo $foo[0]."<br/>";&#xA; echo $foo[1]."<br/>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.