PHP中以天和年为单位的日期差

时间:2019-03-20 14:19:19

标签: php html

所以我有以下表格

<html>
    <body>

        <form action="resp_mensaje.php" method="post">
        <fieldset>
        <legend> Admin:</legend>
        <p><img src="owl.jpg" style="width:250px; height:150px">
        <p>ID:
            <input type="text" name="id" size="40" tabindex="1" autofocus="true" min="1" max="1000" value="" required>
        </p>

        <p>Enrollment Date:
            <input type="date" name="date" size="40" tabindex="3" size="40" autofocus="true" required value="">
        </p>

        <center>
            <button type="submit">
            Calculate
            </button>

            </center>
        </fieldset>
        </form>

    </body>
</html>

“提交”按钮应该向我显示输入信息,并计算自注册日期以来已经过去的天数和年数。关于我该如何做的任何想法?我已经尝试过这样做:

$initialdate = new DateTime($date1); 
$currentdate= new DateTime($date2);
$difference = $initialdate->diff($currentdatel); 
echo .$difference;

但是我收到以下错误消息:  可恢复的致命错误:DateInterval类的对象无法转换为字符串

1 个答案:

答案 0 :(得分:0)

<?php
$start = new DateTime(date('Y-m-d H:i:s'));
$diff = $start->diff(new DateTime(date('Y-m-d- H:i:s)));
$years = $diff->y;
$months = $diff->m;

上面将显示年份和月份中两个日期之间的差异。