我正在编写一个脚本,该脚本检查日期是否增加,以了解是否向现有变量中添加值。 尽管我已经能够在这里获得一些帮助,但看来我仍然无法检查一天是否有所增加。
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>
答案 0 :(得分:1)
每天计算都没有意义,只要需要就可以进行计算。
$ 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)
答案 1 :(得分:0)
使用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;
}