2014-01-13 33 views
2

我有兩個輸入獲取表格的時間差

<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 

回答

0

如果你使用PHP> = 5.3

使用此:

$_start = new DateTime('02/01/2014 14:58:21'); // or new DateTime($_POST['start']); 
$_stop = new DateTime('02/05/2014 14:58:21'); // or new DateTime($_POST['stop']); 
$interval = $_start->diff($_stop); 
echo $interval->format('%a days'); // Should output "4 days". 

//for (-)ve and (+)ve values, use: 
echo $interval->format('%R%a days'); // Should output "+4 days". 

所以,在你send.php應該看起來像這樣:

<?php 
    $pfw_ip= $_SERVER['REMOTE_ADDR']; 
    $email= addslashes($_GET['email']); 
    // Calculating the difference 
    $_start = new DateTime($_GET['start']); 
    $_stop = new DateTime($_GET['stop']); 
    $interval = $_start->diff($_stop); 
    // Preparing email content 
    $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 ".$interval->format('%R%a days')." 
    ID: $pfw_ip <br/>"; 
    $message .= "</body></html>"; 
    mail($to,$subject,$message,$headers); 
?> 
+0

許多種類的感謝,但隨後如果我在HTML表單把這段代碼,我將需要爲p ass $ interval作爲send.php的一個變量我該如何做?加上datetime1和datetime2是兩個輸入字段,所以我不能將它們聲明爲靜態... –

+0

爲它分配一個變量,如'$ diff = $ interval-> format('%a days');'然後可以通過'$ diff'變量給你的send.php。至於你的其他查詢,我在他們旁邊的註釋中添加了代碼。 –

+0

親愛的哦:)。我已經在原文中添加了我的完整代碼 –

0

最簡單的方法是將日期轉換爲Unix時間戳

$start = strtotime($_POST['START']); 
$end = strtotime($_POST['END']); 
$interval = $end - $start; 

,因爲答案都在幾秒鐘內,你可以做簡單的 數學拿到的天數

$days = $interval/86400 // number of seconds in a day 

你也可以看到,如果日期是在一個小時內

如果($間隔> 3600),等等

+0

我的表單使用get方法,如果我添加在send.php $ stop = strtotime($ _ POST ['stop']);它返回一個空白的 –

+0

替換$ _POST,帶有$ _GET或$ _REQUEST,它與兩個 –