简体   繁体   中英

Date difference in days and years in PHP

so I have the following form

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

The submit button is supposed to show me the input info and calculate the number of days and years that have passed since the enrollment date. Any idea on how I could do the latter? I've tried doing it like so:

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

But I get the following error message: Recoverable fatal error: Object of class DateInterval could not be converted to string

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

Above will show the difference between two dates in years and months.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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