繁体   English   中英

获取表单日期差异

[英]Get form date difference

我有一个带有两个输入的 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>

我的send.php

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

如果您使用的是 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 = "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);
?>

最简单的方法是将日期转换为 unix 时间戳

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

因为答案是在几秒钟内,你可以做简单的数学计算来得到天数

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

您还可以查看日期是否在一小时内

if ($interval > 3600),依此类推

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM