【发布时间】:2013-07-22 16:08:39
【问题描述】:
我从 mysql 查询中获取日期,格式为 YYYY-MM-DD。
我需要确定这是否比当前月份过去超过三个月。
我目前有这个代码:
$passwordResetDate = $row['passwordReset'];
$today = date('Y-m-d');
$splitCurrentDate = explode('-',$today);
$currentMonth = $splitCurrentDate[1];
$splitResetDate = explode('-', $passwordResetDate);
$resetMonth = $splitResetDate[1];
$diferenceInMonths = $splitCurrentDate[1] - $splitResetDate[1];
if ($diferenceInMonths > 3) {
$log->lwrite('Need to reset password');
}
问题在于,如果当前月份是 1 月,例如,给出月份值为 01,而 $resetMonth 是 11 月,则给出月份值为 11,那么 $differenceInMonths 将是 -10 ,它不会通过 if() 语句。
如何解决此问题以允许前一年的几个月? 或者有没有更好的方法来完成整个例程?
【问题讨论】:
-
您可以使用DATEDIFF()在查询中选择天数差异
-
我相信您可以在 MySQL 中使用 (EDIT:) DATEDIFF() 执行此操作,但如果您必须在 PHP 中执行此操作:stackoverflow.com/questions/2040560/…