【问题标题】:Get form date difference获取表格日期差异
【发布时间】:2014-01-13 13:15:18
【问题描述】:

我有一个带有两个输入的 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 HOUR,那么我想多加一天。

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 html get datepicker


    【解决方案1】:

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

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

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

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

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

    如果 ($interval > 3600),以此类推

    【讨论】:

    • 我的表单使用 get 方法,如果我在 send.php 中添加 $stop= strtotime($_POST['stop']);它返回一个空白
    • 将 $_POST 替换为 $_GET 或 $_REQUEST,两者都适用
    【解决方案2】:

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

    【讨论】:

    • 非常感谢,但是如果我将此代码放入 HTML 表单中,我需要将 $interval 作为变量传递给 send.php 我该怎么做?加上 datetime1 和 datetime2 是两个输入字段,所以我不能将它们声明为静态...
    • 为其分配一个变量,例如$diff = $interval-&gt;format('%a days');,然后您可以将$diff 变量传递给您的send.php。至于你的其他查询,我已经在他们旁边的 cmets 中添加了代码。
    • 非常感谢 ohhmee 的帮助。仍然有一个错误。现在我收到致命错误:未捕获的异常 'Exception' 带有消息 'DateTime::__construct() [datetime.--construct]: 无法解析时间字符串( 22/01/2014 15:30:18) 在位置 0 (2):/home/site/public_html/send.php:4 中的意外字符' 堆栈跟踪:#0 /home/site/public_html/send.php( 4): DateTime->__construct('22/01/2014 15:3...') #1 {main} 在 /home/site/public_html/sende.php 第 4 行抛出
    • 再次感谢您的帮助,我非常感谢您尝试帮助我。如果您有兴趣我有一些可以合作的项目以供将来参考,您可以在 Facebook 上添加我facebook.com/cristina.anghel.716195?ref=tn_tnmn
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2018-02-09
    • 2017-04-04
    • 2014-01-05
    • 1970-01-01
    相关资源
    最近更新 更多