0

I want to check if my date isn't greater than today. I write this code but it isn't work:

public function getDate() {
    $month = $this->month;
    return $this->year . '-' . $month . '-' . $this->day;
}

public function getDbFormatedDate() {
    if (checkdate($this->month, $this->day, $this->year) || $this->strtotime(getDate())>strtotime(date("Y-m-d"))){
        $dateDeadline    = date_create($this->getDate());
        $this->BirthDate = date_format($dateDeadline, 'Y-m-d');

        Yii::$app->session->setFlash('success', Yii::t('app', 'Zmiany w profilu zostały zapisane'));
    } else {
        Yii::$app->getSession()->setFlash('error', 'nie ma takeij daty');
    }
}
Informer
  • 195
  • 1
  • 13

2 Answers2

2

Use CURDATE() + INTERVAL 2 DAY

SELECT * FROM `table` WHERE date > CURDATE() + INTERVAL 2 DAY

This should do the trick!

Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
2

It is better to compare two DateTime objects. Get inspiration here: https://stackoverflow.com/a/32642436/2709029

function isFuture(DateTime $futureDate)
{
  return ($futureDate > new DateTime) ? TRUE : FALSE;
}

Review from xCrZx:

function isFuture(DateTime $futureDate)
{
  return $futureDate > new DateTime;  // comparison results in boolean
}

Obviously, the ternary operator is redundant. This review was rejected by others and I cannot accept it by myself, although I consider it smarter than my code. So I post it here this way. Anyway, credits go to xCrZx.

Community
  • 1
  • 1
rsl
  • 89
  • 5