我的表单上有条件计算订单的交付时间,遵循以下规则:
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)));
};
我需要的是创建一个规则,其中$ HoraServico基于交付价值$ entrega
示例:如果运费为7.00美元,那么交货时间将是当前时间($ Agora)+ 120分钟的总和。
如果运费为2.00美元,那么交货时间将是当前时间($ Agora)+ 30分钟的总和。等等。
这个想法是,交付率越高(因为它太远),交付成本的时间就越多。
我测试过这样,但它不会发生!他只添加了这一行:
$HoraServico = date('H:i:s', strtotime('+69 minute', strtotime($Agora)));
即,总是加69分钟
据我了解,他没有正确计算实际从这一行获取信息的交付率:
$entrega = $_POST["taxadeentrega"];
对不起英语,我是巴西人,我在使用翻译工具,在葡萄牙语社区,没有人回答!...
只是为了解你,它遵循计算货运价值的部分代码,这段摘录在JS文件中。
}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;
}
答案 0 :(得分:0)
代码中有一个简单但常见的错误:
if ( $entrega = '5.00') {
这不正确。这是为了赋值5.00,而不是检查是否相等。
这应该是:
if ( $entrega == '5.00') {
所有比较都犯了同样的错误。
此代码也不是DRY。一遍又一遍地重复相同的代码行:
$HoraServico = date('H:i:s', strtotime('+30 minute', strtotime($Agora)));
如果时间增量(即' +30分钟')是在if-then-else中设置的变量,或者在switch语句中更好,那会更好,因为这些是所有相互排斥的。
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)));
答案 1 :(得分:0)
如果我不得不重写这段代码,那就像是:
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);