我有兩個輸入獲取表格的時間差
<input type="datetime" name="start" id="start" ></input>
<input type="datetime" name="stop" id="stop" ></input>
和我郵件的形式內容的send.php一個HTML表單。
在send.php我想計算STOP和START之間的天數差異,如差異= STOP - START。如果時間之間的差異是正數並且大於1小時,那麼我希望增加一天額外的時間。
If START = 02/01/2014 14:58:21
If STOP = 07/01/2014 14:59:21
然後輸出=5天
If START = 02/01/2014 14:58:21
If STOP = 07/01/2014 14:59:21
然後輸出=6天
我怎麼能做到這一點?
send.php應通過郵件發送結果。
所以在我的.html我有類似
<form action="send.php" method="get">
<div id="datetimepicker" class="input-append date">
<input type="datetime" name="START" id="START" ></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
<div id="datetimepicker" class="input-append date">
<input type="datetime" name="STOP" id="STOP" ></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
</form>
and my send.php is
<?php
@$pfw_ip= $_SERVER['REMOTE_ADDR'];
@$START= $_GET['START'];
@$STOP= $_GET['STOP'];
@$email= addslashes($_GET['email']);
$_start = new DateTime($_POST['start']); // or new DateTime($_POST['start']);
$_stop = new DateTime($_POST['stop']);// or new DateTime($_POST['stop']);
$interval = $_start->diff($_stop);
$to = "[email protected]";
$subject = "Calculator";
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "Return-Path: $email\r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$message = "<html><body style=\"font-family:Calibri; font-size:14.5px;\">";
$message .= " result between $STOP and $START is ....days
ID: $pfw_ip <br/>";
$message .= "</body></html>";
mail($to,$subject,$message,$headers);
?>
the error i get now is Catchable fatal error: Object of class DateInterval could not be converted to string in
許多種類的感謝,但隨後如果我在HTML表單把這段代碼,我將需要爲p ass $ interval作爲send.php的一個變量我該如何做?加上datetime1和datetime2是兩個輸入字段,所以我不能將它們聲明爲靜態... –
爲它分配一個變量,如'$ diff = $ interval-> format('%a days');'然後可以通過'$ diff'變量給你的send.php。至於你的其他查詢,我在他們旁邊的註釋中添加了代碼。 –
親愛的哦:)。我已經在原文中添加了我的完整代碼 –