我想获得一个用户注册日期,计算自注册以来的天数并显示给定天数剩余的最大天数并在期间结束后显示消息。我到目前为止的工作:$regDatestr = '2020-04-09 19:38:10';$regDate = strtotime($regDatestr);$regSince = time() - $regDate;$days = round ($regSince / ( 60 * 60 * 24 ));$maxDays = 20;$maxDaysstr = strtotime("-$maxDays days");$maxReg = ($regSince + $maxDaysstr); $daysleft = time() - $maxReg;$restDays = round ($daysleft / ( 60 * 60 * 24 ));if ($regdate <= $maxDaysstr) : echo 'period ended'else : echo 'Registered since' . $days . ' .days . Rest days ' . $restDays . 'endif;$daysleft 和 days 给了我正确的日子。但这段时间并没有在 20 天后准确结束。我需要的是自注册日期起最多 20 天。所以当'2020-04-09 19:38:10';加上 20 天应该在该期间结束'2020-04-29 19:38:10';但似乎我的 if 条件没有按预期工作。所以我得到“从 21 天开始注册。休息日 0”。为什么?
1 回答
海绵宝宝撒
TA贡献1809条经验 获得超8个赞
因为你的变量名有错字
if ($regdate <= $maxDaysstr)
应该
if ($regDate <= $maxDaysstr)
你的代码可以更短
$regDatestr = '2020-04-01 19:38:10';
$regDate = new DateTime($regDatestr);
$diffToday = $regDate->diff(new DateTime());
$maxDays = 10;
$diffMax = $regDate->diff(new DateTime("-$maxDays days"));
if ($diffMax->invert == 0) :
echo 'period ended';
else :
echo 'Registered since ' . $diffToday->days . ' .days . Rest days ' . $diffMax->days;
endif;
- 1 回答
- 0 关注
- 63 浏览
添加回答
举报
0/150
提交
取消