Am working on a project where a database row will be updated every 2 minutes and I have written a code for it but the problem is when I refresh the page I have included the code to confirm if the database row is been updated after every 2 minutes, I discovered the database row gets updated even before 2 minutes.
$plan_amount = 100; // plan amount
$inc_profits = 0.05 * $plan_amount; // increase profit by 5%
// Get current timestamp
$currentTime = time();
// Calculate next 2 mins timestamp
$next24Hours = strtotime($plan_amount_time) + (2 * 60);
// Check if it's time to execute the query
if ($currentTime >= $next24Hours) {
// SQL query to insert data into the database
$insert = "UPDATE users SET active_plan_amount_time = CURRENT_TIMESTAMP, profits = profits + ? WHERE id = ?";
$stmt = mysqli_prepare($db, $insert);
mysqli_stmt_bind_param($stmt, "ii", $inc_profits, $id);
mysqli_stmt_execute($stmt);
} else {
// 2 mins have not passed yet. No action taken
}
The above code is the code I have written to solve the problem but I still face difficulties. Note that $plan_amount_time is a row with the current timestamp of when last the row was updated.
I updated $plan_amount_time with default timestamp which the code will use to check if current time passed is 2 mins but I still face the same problem.