我想检查我的日期是否不大于今天。我写了这段代码,但它不起作用:
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');
}
}
使用 CURDATE() + 间隔 2 天
SELECT * FROM `table` WHERE date > CURDATE() + INTERVAL 2 DAY
这应该可以解决问题!
最好比较两个日期时间对象。在这里获得灵感: https://stackoverflow.com/a/32642436/2709029
function isFuture(DateTime $futureDate)
{
return ($futureDate > new DateTime) ? TRUE : FALSE;
}
来自xCrZx的评论:
function isFuture(DateTime $futureDate)
{
return $futureDate > new DateTime; // comparison results in boolean
}
显然,三元运算符是多余的。这个评论被其他人拒绝了,我自己不能接受它,尽管我认为它比我的代码更聪明。所以我以这种方式在这里发布它。无论如何,积分归xCrZx所有。