我有一个带有两个输入的html表单
<input type="datetime" name="start" id="start" ></input>
<input type="datetime" name="stop" id="stop" ></input>
和send.php,它将表格内容邮寄给我。
在 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 = "me@me.com";
$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 :(得分:0)
如果您使用的是PHP&gt; = 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 = "me@me.com";
$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);
?>
答案 1 :(得分:0)
最简单的方法是将日期转换为unix时间戳
$start = strtotime($_POST['START']);
$end = strtotime($_POST['END']);
$interval = $end - $start;
因为答案是几秒钟,你可以做到简单 数学来获得天数
$days = $interval / 86400 // number of seconds in a day
您还可以查看日期是否在一小时内
if($ interval&gt; 3600),等等