doufu9521 2013-11-12 09:03
浏览 14
已采纳

如何计算从出勤时间到PHP的休假时间

i have simple script that suppose if i attendance at 9:00 oclock it will be add 8:30 hours (this is work time) + 00:30 (this is break time). and it will print the leaving time it will be (6:00) but it not work know why :

<form id="form1" id="form1" action="mail.php" method="POST">

    <label>time
        <span class="small">Add your attendance time</span>
    </label>
    <input type="text" name="time" value="9:00" >
    <label>Email
        <span class="small">Enter a Valid Email</span>
    </label>
        <input type="text" name="email" value="amd@dt4it.com">
<br />
<br />

    <label>break  
        <span class="small">break </span>
    </label>

<select name="break" size="1">
<option value="00:30">yes</option>
<option value="00:00">no</option>

</select>
<br />
<br />
<br />

    <button type="submit" value="Send" style="margin-top:15px;">Submit</button>
<div class="spacer"></div>

</form>

<?php
$time = $_POST['time'] + $_POST['break'] ;
$email = $_POST['email'];
$formcontent=" From: $time ";
$recipient = $email;
$subject = "Contact Form";
$mailheader = "From: $email 
";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";

$time2 = strtotime($time)+ strtotime("08:30");
$result = date("H:i",strtotime($time2));

echo $result;
?>
  • 写回答

1条回答 默认 最新

  • doumei1908 2013-11-12 09:25
    关注

    The easier method would be to work with DateInterval, see <option> values, like:

    <form action="" method="POST">
        <label>Add your attendance time</label>
        <input type="text" name="time" value="9:00" >
        <label>Email</label>
        <input type="text" name="email" value="amd@dt4it.com">
        <label>Break</label>
        <select name="break" size="1">
            <option value="PT30M">yes</option>
            <option value="PT00M">no</option>
        </select>
        <input type="submit" value="Send" />
    </form>
    

    Then the PHP part becomes even easier, like:

    if ($_POST) {
        // create DateTime object for "time"
        $time = new DateTime($_POST['time']);
    
        // add break to "time" DateTime object
        $interval = new DateInterval($_POST['break']);
        $time->add($interval);
    
        // add 8:30 to "time" DateTime object
        $interval = new DateInterval("PT8H30M");
        $time->add($interval);
    
        echo $time->format('H:i \o g:i A');
        // will output smth like: 18:00 or 6:00 PM / 17:30 or 5:30 PM
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?