PHP - 如何根据两个POST日期显示年,月和日?

时间:2021-06-25 21:32:04

I am testing out whether I can get and display the duration that is between two dates based on post. These two dates are called from the database.

我正在测试我是否可以根据帖子获得并显示两个日期之间的持续时间。这两个日期是从数据库中调用的。

This is before I clicked the "Update Contract". PHP  - 如何根据两个POST日期显示年,月和日?

这是在我点击“更新合同”之前。

This is after I clicked the "Update Contract". PHP  - 如何根据两个POST日期显示年,月和日?

这是在我点击“更新合同”之后。

Php:

<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
error_reporting( ~E_NOTICE );

if(isset($_POST['edit']) ){     
    $startdate = date('Y-m-d', strtotime($_POST['startdate']));
    $expdate = date('Y-m-d', strtotime($_POST['expdate']));
    $diff = abs(strtotime($expdate) - strtotime($startdate));

    $years = floor($diff / (365*60*60*24));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

    $upd= "UPDATE `contracts` SET  `startdate` = ?, `expdate` = ? WHERE `id` = ?";
    $stmt = $con->prepare($upd);
    $stmt->bind_param("ssi",$startdate,$expdate,$id);
    $stmt->execute();

        if ($stmt->errno){
        echo "FAILURE!!! " . $stmt->error;
        } else {
        $successMsg =  "Contract Successfully Updated!";
        }
        $stmt->close();     

    }

printf("%d years, %d months, %d days\n", $years, $months, $days);
?>

Form:

<form method="post" action="">
    <?php
    if(isset($successMsg)){
    ?>
    <div class="alert alert-success">
    <strong><?php echo $successMsg; ?></strong>

    <?php
    }
    ?>
    <?php

    $id = filter_input(INPUT_GET, 'id');
    $sql = "SELECT * FROM contracts WHERE `id` = $id";
    $result = $con->query($sql);
    $row = $result->fetch_assoc();  

    ?>
        <label>Start Date</label>
        <input type="date" name="startdate" value="<?php echo $row['startdate']; ?>"/>

        <label>Expiry Date</label>
        <input type="date" name="expdate" value="<?php echo $row['expdate']; ?>"/>                              

        <button type="submit" class="btn btn-success btn-md" name="edit">
        <span class="glyphicon glyphicon-pencil"></span> Update Contract
        </button>
</form>

The Problems are:

问题是:

  1. How can I display the duration without having to clicked on the "Update Contract" to see the updated duration?
  2. 如何显示持续时间而无需单击“更新合同”以查看更新的持续时间?

  3. The updated duration between the dates shown are not the exact duration as it is supposed to be 1 year, 11 months, 3 days.
  4. 所显示日期之间的更新持续时间不是确切的持续时间,因为它应该是1年,11个月,3天。

1 个解决方案

#1


0  

Answer for second question:

回答第二个问题:

make use of the native DateTime Class and it's related DateInterval:

使用本机DateTime类及其相关的DateInterval:

<?php
$startdate=new DateTime($_POST['startdate']); 
$expdate=new DateTime($_POST['expdate']); 
$diff=$expdate->diff($startdate); 
print_r( $diff );
// or 
printf("%d years, %d months, %d days\n", $diff->y, $diff->m, $diff->d);

// for database input you'll have to format the dates like so:
echo $startdate->format('Y-m-d');

The solution to your first question is just about restructuring your code.
I suppose this is all in one php-file.

第一个问题的解决方案就是重构代码。我想这都是一个php文件。

  • check whether your form was posted, as you did.
  • 检查您的表单是否已发布,就像您一样。

  • if so, do the database update
  • 如果是,请进行数据库更新

  • if not, get the values from database - and set the same variable names ($startdate = $row['startdate'];)
  • 如果没有,从数据库中获取值 - 并设置相同的变量名称($ startdate = $ row ['startdate'];)

  • in any case do the calculation, as above
  • 在任何情况下都进行计算,如上所述

  • then display what you need (the difference, the form, a message...)
  • 然后显示你需要的东西(差异,形式,信息......)

#1


0  

Answer for second question:

回答第二个问题:

make use of the native DateTime Class and it's related DateInterval:

使用本机DateTime类及其相关的DateInterval:

<?php
$startdate=new DateTime($_POST['startdate']); 
$expdate=new DateTime($_POST['expdate']); 
$diff=$expdate->diff($startdate); 
print_r( $diff );
// or 
printf("%d years, %d months, %d days\n", $diff->y, $diff->m, $diff->d);

// for database input you'll have to format the dates like so:
echo $startdate->format('Y-m-d');

The solution to your first question is just about restructuring your code.
I suppose this is all in one php-file.

第一个问题的解决方案就是重构代码。我想这都是一个php文件。

  • check whether your form was posted, as you did.
  • 检查您的表单是否已发布,就像您一样。

  • if so, do the database update
  • 如果是,请进行数据库更新

  • if not, get the values from database - and set the same variable names ($startdate = $row['startdate'];)
  • 如果没有,从数据库中获取值 - 并设置相同的变量名称($ startdate = $ row ['startdate'];)

  • in any case do the calculation, as above
  • 在任何情况下都进行计算,如上所述

  • then display what you need (the difference, the form, a message...)
  • 然后显示你需要的东西(差异,形式,信息......)