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

时间:2013-11-12 09:03:56

标签: php datetime

我有一个简单的脚本,假设如果我在上午9点出席,它将在8:30时加入 (这是工作时间)+ 00:30(这是休息时间)。它会打印离开时间 这将是(6:00) 但它不起作用知道原因:

<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 \r\n";
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 个答案:

答案 0 :(得分:1)

更简单的方法是使用DateInterval,查看<option>值,例如:

<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>

然后PHP部分变得更加容易,例如:

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\r g:i A');
    // will output smth like: 18:00 or 6:00 PM / 17:30 or 5:30 PM
}