0

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

James Coyle
  • 9,922
  • 1
  • 40
  • 48
Stephanie
  • 9
  • 4
  • 3
    What have you tried? Add your attempted PHP code to your question. – Jonnix Mar 20 '19 at 14:21
  • 4
    Hi, I see you're pretty new to StackOverflow.You need to show us your php code that processes this form. If you are in fact just asking for that code, then it's not really an appropriate question as you should be attempting it first and then asking for help on specific problems – TommyBs Mar 20 '19 at 14:21
  • 1
    Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Russ J Mar 20 '19 at 14:25
  • Doing this. But I'm getting an error message : Recoverable fatal error: Object of class DateInterval could not be converted to string $initialdate = new DateTime($date1); $currentdate= new DateTime($date2); $difference = $initialdate->diff($currentdatel); – Stephanie Mar 20 '19 at 14:35

1 Answers1

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;

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

Anoxy
  • 873
  • 7
  • 17
  • Why add the `date('Y-m-d- H:i:s')`? – Jonnix Mar 20 '19 at 14:29
  • @Jonnix Just to get a date in the example. Could be anything – Anoxy Mar 20 '19 at 14:30
  • So just `new DateTime` would do, rather than adding an extra unneeded function call. – Jonnix Mar 20 '19 at 14:31
  • @Jonnix It would but there would be no format of date shown. If you consider the answer wrong feel free to downvote. – Anoxy Mar 20 '19 at 14:32
  • All I'm saying it that it could cause confusion for people reading. What you've written is adding complexity to a simple solution, for no discernible gain. If you want to create an instance of DateTime from a specific format you'd use createFromFormat, not date(), and if you wanted a DateTime for now, you'd not pass an arg at all. – Jonnix Mar 20 '19 at 14:36
  • 1
    Listen, the date is there. I doubt this is how the OP would use it as he wants diff in years and months, and hey, there is no diff between now and now. So once he fills in his own values it works. I agree with you that if you would just use the snippet as is. Then it would be unecessary code. But the snippet is meant for a paper and glue solution. He gets the paper and glues on his own things. – Anoxy Mar 20 '19 at 14:39