dongren5293 2016-10-18 19:15
浏览 239
已采纳

代码没有产生正确的输出

I've tried to create a program that allows the user to input a time/duration (hh:mm). If this time/duration exceeds one hour, then 15 minutes are added to a variable called 'MaintenanceDuration'. If their input is less than or equal to an hour, 10 minutes are added.

When I run the code it only ever adds 10 minutes, despite the input being greater than an hour.

I've been told its because $Duration is not set/defined however surely it is when the user submits their input?

HTML:

<html>
<body>

<form action="maintenance-formula.php" method="post">
Duration: <input type="time" name="Duration" id="Duration"><br>
<input type="submit">
</form>

</body>
</html>

PHP:

<?php

$Duration = $_POST['Duration'];

if ($Duration > strtotime('1:00:00')) {
        echo "Added 15 maintenance minutes!";
        $MaintenanceDuration = strtotime('0:15:00');
} else {
        echo "Added 10 maintenance minutes!";
        $MaintenanceDuration = strtotime('0:10:00');
}

$MaintenanceDuration = strftime('%H:%M', $MaintenanceDuration);
echo $MaintenanceDuration;

?>

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongxuan58311366668 2016-10-18 19:23
    关注

    if $Duration is strictly given in hh:mm format, you've gotta parse it.

    $parsedDuration = explode(':', $Duration);
    
    $hours = intval($parsedDuration[0]);
    $mins  = intval($parsedDuration[1]);
    
    if ($hours > 0 && $mins > 0) {
        echo "Added 15 maintenance minutes!";
        $MaintenanceDuration = strtotime('0:15:00');
    } else {
        echo "Added 10 maintenance minutes!";
        $MaintenanceDuration = strtotime('0:10:00');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器