-3

I'm trying to figure out how to code a weekly login rewards and so far I'm a little bit stuck with the current logic I have.

Assuming I have 2 sql tables; 1) for members which has the login session date and 2) for the rewards. So far this is the code I somewhat managed to come up with, and if there is something much better than this, it'll be much highly appreciated. ;__;

// DO CHECK LOGIN
$sql = mysqli_query($connect, "SELECT * FROM `members` WHERE email='$login'");
$row = mysqli_fetch_assoc($sql);

$sql2 = mysqli_query($connect, "SELECT * FROM `rewards` WHERE email='$login'");
$row2 = mysqli_fetch_assoc($sql2);

if ($row2['week'] == 0) {
    echo 'This is your first weekly login bonus! Get your rewards below:';
    // INSERT REWARD FIRST WEEK
    mysqli_query($connect, "UPDATE `rewards` SET `week`='1', `login`='".date('Y-m-d H:i:s')."' WHERE email='$login'");
} else if (($row2['week'] == 1) && (/* $row['session'] is 7 days ahead of $row2['login']*/)) {
    echo 'Get your weekly login bonus below.';
    // INSERT REWARD SECOND WEEK
    mysqli_query($connect, "UPDATE `rewards` SET `week`='2', `login`='".date('Y-m-d H:i:s')."' WHERE email='$login'");
} else if (($row2['week'] == 2) && (/* $row['session'] is 7 days ahead of $row2['login'] (14 days)*/)) {
    echo 'Get your weekly login bonus below.';
    // INSERT REWARD THIRD WEEK
    mysqli_query($connect, "UPDATE `rewards` SET `week`='3', `login`='".date('Y-m-d H:i:s')."' WHERE email='$login'");
} else if (($row2['week'] == 3) && (/* $row['session'] is 7 days ahead of $row2['login'] (21 days)*/)) {
    echo 'Get your weekly login bonus below.';
    // INSERT REWARD FOURTH WEEK
    mysqli_query($connect, "UPDATE `rewards` SET `week`='0', `login`='".date('Y-m-d H:i:s')."' WHERE email='$login'"); // WEEK RESETS FOR NEXT LOGIN
} else {
    if ($row2['login'] /* is X days less than $row['session'] */) {
        echo 'Your next weekly reward is on [insert date].';
    } else if ($row['session'] /* is 8 days more than $row2['login'] */) {
        echo 'It seems like you missed a week\'s login. Start over again?';
        mysqli_query($connect, "UPDATE `rewards` SET `week`='0', `login`='".date('Y-m-d H:i:s')."' WHERE email='$login'");
    }
}

So I'm not entirely sure how I'm gonna execute the commented part of each if-else statement (besides the INSERT REWARD part) because I'm a little bit lost. Many many thanks in advance to those who will be able to help out!

Aki
  • 85
  • 1
  • 8
  • 4
    "I'm a little bit lost" isn't really an answerable question. If the overall scope of the problem is too much to understand at once, break it down into smaller pieces. Attempt each piece individually. As you dissect your goals and build your components, eventually you should either (A) build a solution or (B) encounter a specific step which either produces an unexpected result or which you have no idea how to approach. We can help with that step. – David Sep 19 '19 at 17:02
  • 3
    [Little Bobby](http://bobby-tables.com/) says [you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/). Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). – GrumpyCrouton Sep 19 '19 at 17:02
  • I don't really see a question here that we could answer without knowing everything about your application that you do. – GrumpyCrouton Sep 19 '19 at 17:03
  • I assume what you are looking for is how to calculate the time offsets? – Tox Sep 19 '19 at 17:10
  • @GrumpyCrouton "how I'm gonna execute the commented part of each if-else statement" is supposed to be the question for this: $row['session'] is 7 days ahead of $row2['login'] -- I'm not sure how to write it to a code. I'm sorry ^^; – Aki Sep 19 '19 at 21:39
  • Hi @Tox, umm.. I think so? Will it answer this logic: $row['session'] is 7 days ahead of $row2['login'] for the if-else statement? (e.g. if we have 2019-09-20 for the session, and 2019-09-15 for the login) – Aki Sep 19 '19 at 21:48

1 Answers1

0

PHP has many ways to deal with time, my preferred approach is to convert it to Unix time first. That is the number of seconds elapsed since Jan 1st 1970. Now it's 1568953527 in Unix time, for example.

Here's some example code for one way you can use to deal with time and dates:

$now = time(); // = 1568953527
$one_day = 60 /* seconds */ * 60 /* minutes */ * 24 /* hours */; // = 86400
$tomorrow = $now + $one_day; // 1569039927
$next_week = $now + $one_day * 7; // 1569558327

To be able to use it multiple times without duplicating your code you can put that in a function:

function is_not_older_than_one_week($time)
{
  $now = time();
  $one_day = 60 * 60 * 24;
  $one_week_ago = $now - $one_day * 7;

  return $time >= $one_week_ago;
}

$not_older_than_a_week = is_not_older_than_one_week(1568953527); // at the time of writing this is true, in 8 days from now it will be false

And this is how you can use it with human readable dates:

$date = '2019-09-20';
$unixtime = strtotime($date);
$not_older_than_a_week = is_not_older_than_one_week($unixtime);

You should read this, there is yet another way described how to calculate time offsets.

Hope this helps!

Tox
  • 373
  • 4
  • 13