【发布时间】:2015-11-20 15:28:38
【问题描述】:
我正在制作一个 php 脚本,我将安排它每天早上 9 点运行。
我需要遍历我数据库中卡片表中存储的所有详细信息。在每一行中,我都有一个 expireDate 列,我需要检查该值是否在当前日期的 3 个月内,如果是,则发送一封电子邮件以提醒用户他们的卡即将到期。如果到期日期在一个月内,他们还希望发送第二次提醒。
我遇到的问题是用于检查到期日期是否在当前日期的 3 个月内的比较逻辑。代码如下:
...
if ($Cards = mysqli_stmt_get_result($stmt)){
while($row=mysqli_fetch_array($Cards))
{
$email = $row['email'];
$name = $row['Name'];
$expDate = $row['expDate'];
$reminderSent = $row['reminderSent'];
$timeDiff = (intVal(time()) - intVal($expDate));
echo "curDate is ". time() . " and expDate is ".$expDate. ". Difference is ".(intval(time()) - intVal($expDate));
echo "<br>";
//if ($timeDiff within3months){
// if ($reminderSent == 0) {
// //php code to send email
// }
// else if ($reminderSent == 1 && $timeDiff within1month){
// //php code to send email
// }
//}
}
}
...
我有一个想法,我也许可以找到 3 个月的 epoch 值并将其添加到我的到期日期 val 中并检查 curDate 是否大于此值。这样的东西会起作用吗?
E.G: $expDate + 5097600 > intVal(time())
如果有更好的方法来解决这个问题,任何建议表示赞赏。
【问题讨论】: