I faced such a problem that I have two dates: today and entered by the user and I needed to compare them. The code itself looks like this:

$tim = strtotime(date('Ym-d'))-strtotime($_POST['_bt']); 

But the problem is that if the date is in $ _POST ['_ bt'] before 1.1.1970, then the output simply does not occur.

    1 answer 1

    Use standard DateTime and provide a date calculation for it. Not to mention the fact that this class is more convenient and clearer than manual timestamp control.

     $userInput = \Datetime::createFromFormat('Ym-d', $_POST['_bt']); if (! $userInput) { // todo user input invalid date } var_dump($userInput->diff(new \datetime())); 

    For a simple more-to-less comparison, DateTime implements the corresponding operators.

     if ($userInput > new \DateTime) { // дата из будущего } 
    • I do not really need that. I need to find how many years have passed since the date the user entered, I do it this way: if (floor ($ live / (60 * 60 * 24 * 366)) <= 5) {// code here} But I don’t I can enter a date longer than 46 years! - Maxim Korolev
    • 2
      Use this answer, you can output the number of years through the function format - echo $ userInput-> diff (new DateTime ()) -> format ('% Y% years'); respectively documentation php.net/manual/ru/dateinterval.format.php - Daniel Protopopov