0

I have a condition on my form that calculates the delivery time of the orders, follows the rule below:

date_default_timezone_set('America/Sao_Paulo');
$Agora = date('H:i:s');
$HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));

if ( $entrega == '5.00'){
    $HoraServico = date('H:i:s', strtotime('+120 minute', strtotime($Agora)));
}

if ( $entrega == '3.80'){
    $HoraServico = date('H:i:s', strtotime('+30 minute', strtotime($Agora)));

}

else if  ( $entrega == '7.0'){
    $HoraServico = date('H:i:s', strtotime('+20 minute', strtotime($Agora)));

};

What I need is to create a rule where $HoraServico is based on the delivery value $entrega

Example: If the delivery fee costs $ 7.00 then the delivery time will be the sum of the current time ($Agora) + 120 minutes.

If the delivery fee costs $ 2.00 then the delivery time will be the sum of the current time ($Agora) + 30 minutes. And so on.

The idea is, the more expensive the delivery rate (because it is far) the more time it will cost to deliver.

I tested like this, but it's not going! He is adding only the line:

$HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));

That is, always adding 69 minutes

As I understand it, he is not computing correctly the delivery rate that actually takes information from this line:

$entrega = $_POST["taxadeentrega"];

Sorry for the English, I'm Brazilian and I'm using a translator, in the community in Portuguese, nobody has answer!...

Just for the knowledge of you, it follows part of the code that calculates the value of the freight, this excerpt is in a JS file.

    }if(cep == "20010-020" || cep == "20011-020" || cep == "20011-030" || cep == "20011-040" || cep == "20011-901" || cep == "20020-000" || cep == "20010-170" || cep == "20011-000" || cep == "20021-260" || cep == "20031-040" || cep == "20031-050" || cep == "20031-130" || cep == "20031-204" || cep == "20021-245" || cep == "20040-002" || cep == "21335-253" || cep == "20040-031" || cep == "20030-041" || cep == "20040-000" || cep == "20004-002" || cep == "20040-003" || cep == "20040-007" || cep == "20030-042" || cep == "20011-010"){

        //se for um dos ceps acima, incrementa 1.7 no valor final
        taxa = 2.00;


    }if(cep == "20010-090" || cep == "20020-100" || cep == "20021-130" || cep == "20021-315" || cep == "20030-001" || cep == "20031-000" || cep == "20031-003" || cep == "20031-010" || cep == "20031-050" || cep == "20031-141" || cep == "20031-143" || cep == "20031-005" || cep == "20031-001" || cep == "20020-903" || cep == "20031-144" || cep == "20030-080" || cep == "20031-142" || cep == "20031-120" || cep == "20031-007" || cep == "20010-009" || cep == "20031-913" || cep == "20021-370" || cep == "20200-100" || cep == "20030-901" || cep == "20030-021" || cep == "20210-030" || cep == "24220-280"){

        //se for um dos ceps acima, incrementa 1.2 no valor final
        taxa = 1.50;


    }if(cep == "20020-010"|| cep == "22050-032" || cep == "20020-040" || cep == "20020-080" || cep == "20021-060" || cep == "20021-120" || cep == "20021-900" || cep == "20021-903" || cep == "20030-002" || cep == "20030-015" || cep == "20030-013" || cep == "20030-020" || cep == "20030-021" || cep == "20030-060" || cep == "20030-070" || cep == "20030-120" || cep == "20002-080" || cep == "20002-008" || cep == "20003-021" || cep == "20030-905" || cep == "24220-031" || cep == "20002-010" || cep == "20030-015"){

        //se for um dos ceps acima, incrementa 0.7 no valor final
        taxa = 1.00; 

    }

total += taxa;      

if(taxa != 0){

//caso a taxa seja diferente de 0, mostra ao usuário
    document.getElementById("idTaxa").innerHTML = "Additional charge R$ " + taxa;
}
2
  • Four things to start with: 1. Why do you store amounts of money in strings? 2. You should never check whether two floating points are exactly equal, they rarely are. 3. You compare two values with == because = is an assignment. 4. One else seems to be missing. Aug 30, 2017 at 14:12
  • I have in this form a function, where for each CEP has a value to be charged, what I want now is based on this value, be added a time. I am a layman, in the Portuguese community I have been given this code, which is not working, if you can help me I will be very grateful. Aug 30, 2017 at 14:16

2 Answers 2

0

If I had to rewrite this code it would be something like:

date_default_timezone_set('America/Sao_Paulo');

function deliveryTime($shippingCost)
{
  $delay = 69; // minutes of delay in the delivery
  if ($shippingCost >= 2.00) $delay += 30; // added minutes when shipping is over 2
  if ($shippingCost >= 5.00) $delay += 30; // added minutes when shipping is over 5
  if ($shippingCost >= 7.00) $delay += 30; // added minutes when shipping is over 7
  return date('H:i:s', strtotime('+'.$delay.' minute'));
}

$horaServico = deliveryTime($entraga);
5
  • Thanks for listening! It still continues to add up to only 69 minutes, I do not know why, but it is not recognizing the rule. Aug 30, 2017 at 15:29
  • Time to check the variables. You can do this by placing echo <variable> in strategic places. I would like to know the content of $entraga. Aug 30, 2017 at 15:52
  • This delivery rate ($ delivery) is loaded through this code: $entrega = $_POST["taxadeentrega"]; In the file form.js has a function that calculates the value of the taxe of delivery through the CEP and shows this in the form across the field taxadeentrega. Aug 30, 2017 at 16:05
  • If there really is an $ sign in $entraga then it is not a number and cannot be used in a numerical comparison. We would have to remove the $ sign first. Aug 30, 2017 at 18:07
  • No, it does not have the $ symbol it's just number Aug 30, 2017 at 19:25
0

There is a simple but common mistake in the code:

if ( $entrega = '5.00') {

This is not correct. This is making an assignment of the value of 5.00 rather than checking for equality.

This should be:

    if ( $entrega == '5.00') {

The same mistake was made with all the comparisons.

This code is also not DRY. The same line of code is repeated over and over:

 $HoraServico = date('H:i:s', strtotime('+30 minute', strtotime($Agora)));

It would be better if the time increment (ie. '+30 minute') was a variable that was set in the if-then-else, or better yet in a switch statement, since these are all mutually exclusive.

switch ($entraga) {
    case '5.00':
        $mins = '+120 minute';
        break;
    case '2.00':
        $mins = '+30 minute';
        break;
    case '7.00':
        $mins = '+20 minute';
        break;

    default:
        $mins = '+69 minute'
}
date_default_timezone_set('America/Sao_Paulo');
$Agora = date('H:i:s');
$HoraServico = date('H:i:s', strtotime($mins, strtotime($Agora)));
3
  • I put it as you explained, but even so he does not recognize the rule. It is only adding, always, with 69 minutes. From what I'm noticing he is not reading correctly the rule that refers to delivery rate ($entrega) Aug 30, 2017 at 14:29
  • Your code only works if $entraga is being set correctly. That code is not included, but you should debug that by echoing out the value of $entraga and checking it.. With that said, the code could not possibly have worked correctly given the mistake in making assignment vs. logical comparison.
    – gview
    Aug 30, 2017 at 14:32
  • Thanks for listening! It still continues to add up to only 69 minutes, I do not know why, but it is not recognizing the rule. Aug 30, 2017 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.