我正在编写一个脚本来检查日期的增加,以了解是否要向现有变量添加值。虽然我已经能够在这里得到一些帮助,但仍然看起来我仍然坚持检查一天是否增加了。
PHP代码
<?php
$now = time(); // or your date as well
$your_date = strtotime("2018-09-05");
$datediff = $now - $your_date;
$alreadySpentDays = round($datediff / (60 * 60 * 24));
$userEarnings = 0;
// i want to be able to check if the day has increased and if it has i want to be able to add $10 to userEarning variable daily
?>
<p><?=$userEarnings?></p>
每天计算它没有意义,只需在需要时进行计算。 $ start_date是您要开始计算的日期,其中收入为0。 然后计算经过的天数并乘以10。
$now = time(); // or your date as well
$start_date = strtotime("2018-09-04");
$datediff = $now - $start_date;
$alreadySpentDays = round($datediff / (60 * 60 * 24));
echo $alreadySpentDays * 10; // 20, since it's two days of earnings (september 4 and 5)
使用DateTime :: diff(http://php.net/manual/en/datetime.diff.php)
$userEarnings = 0;
$now = new DateTime(); //now
$your_date = new DateTime("2010-09-05");
$diff = $now->diff($your_date);
if ($diff->days > 0) {
$userEarnings += 10;
}