我想獲得一個用戶注冊日期,計算自注冊以來的天數并顯示給定天數剩余的最大天數并在期間結束后顯示消息。我到目前為止的工作:$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 關注
- 92 瀏覽
添加回答
舉報
0/150
提交
取消