0

i am writing a script that checks for increase in date to know whether or not to add a value to an existing variable. Though i have been able to get some help here, it still appears i'm stuck with checking if the day has increased.

PHP CODE

<?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>
Habib
  • 591
  • 8
  • 29
courage
  • 115
  • 3
  • 20
  • 1
    What's the problem that you're running into? It looks like the code you have will roughly tell you if a day has gone by (you may want to use `floor` instead of `round`). This might also be a good opportunity (and reason) to switch to using PHP's [DateTime](https://secure.php.net/manual/en/book.datetime.php) library. Then you can take advantage of things like the `diff` method. [this question](https://stackoverflow.com/questions/18102603/get-a-php-datetime-difference-in-days-considering-midnight-as-a-day-change) might help ease you into `DateTime`. – RToyo Sep 05 '18 at 17:14
  • Actually, getting the days is not the issue, my problem is with knowing when it increases and adding $10 to user daily earning – courage Sep 05 '18 at 17:23
  • `echo '$'.number_format(date('G') * (10 / 24), 2).' earned today!';` [1](https://3v4l.org/M4TNX) then cron job it at a 00:00 to apply.. – Lawrence Cherone Sep 05 '18 at 17:24
  • @courage Where are you storing the user's daily earnings? Is it possible to also store the last date that the user's earnings were evaluated (so that you can see how many days worth of money they've earned since then)? Or do you store the date that the user first began earning daily, so that you can simply say get the difference in days between now and that start date, and multiply it by $10? – RToyo Sep 05 '18 at 17:29
  • I did store the registration date and get the difference between then and the current date – courage Sep 05 '18 at 17:33
  • and i am not multiplying it by 10, i simply want to add $10 to user earnings everyday – courage Sep 05 '18 at 17:45

2 Answers2

1

There is no point in calculating it each day, just do the calculation when needed.
$start_date is the date you want to start counting from, where the earning is 0.
Then just calculate the number of days that has passed by and multiply with 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)
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Use 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;
}
symques
  • 46
  • 7