courage
courage

Reputation: 125

How To Check If A Value is increasing or has increased - PHP

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>

Upvotes: 0

Views: 584

Answers (2)

Andreas
Andreas

Reputation: 23958

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)

Upvotes: 1

symques
symques

Reputation: 46

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;
}

Upvotes: 0

Related Questions