search

Home  >  Q&A  >  body text

How to check time between two date variables in php?

<p>I have 3 variables, which are 3 numbers (day, year, month). Together they become a date type variable. Additionally, I have a date variable that represents the current date. </p> <p>How can I check if the first date is 13 years or more before the current date? Here is my code: </p> <pre class="brush:php;toolbar:false;">$usersday = (int)$_POST['day']; $usersyear = (int)$_POST['year']; $usersmonth = (int)$_POST['month']; $takedate = date_create("$usersyear-$usersmonth-$usersday"); $date = date_format($takedate, 'd-m-Y'); $currentDate = date('Y-m-d');</pre> <p><br /></p>
P粉831310404P粉831310404495 days ago599

reply all(1)I'll reply

  • P粉787934476

    P粉7879344762023-08-17 00:27:41

    The

    date_*() functions and date() are part of two different date library APIs, confusingly. The object-oriented version of the date_*() functions clarifies this to some extent and is also a more powerful library. For example:

    $usersyear = 2001;
    $usersmonth = 1;
    $usersday = 1;
    
    $takedate = new DateTime("$usersyear-$usersmonth-$usersday");
    $today    = new DateTime('today');
    $diff     = $today->diff($takedate);
    
    var_dump($diff, $diff->y);
    

    Output:

    object(DateInterval)#3 (10) {
      ["y"]=>
      int(22)
      ["m"]=>
      int(7)
      ["d"]=>
      int(15)
      ["h"]=>
      int(0)
      ["i"]=>
      int(0)
      ["s"]=>
      int(0)
      ["f"]=>
      float(0)
      ["invert"]=>
      int(1)
      ["days"]=>
      int(8262)
      ["from_string"]=>
      bool(false)
    }
    int(22)

    Reference:https://www.php.net/manual/en/book.datetime.php

    reply
    0
  • Cancelreply