A few days back I asked for help on this website, and after some answers pointed me in the right direction I’ve scratched my last approach and taken a new one on how I create this program.
The new solution works exactly the way I want it to, however, I am not too confident that it’s written the best way. I’m a beginner, so the code took many attempts before it worked.
Basically all I’m trying to do is display an HTML table with a list of dates and date-differences from a database, the values coming from input fields on the webpage.
If possible, would anyone be able to tell me if my code is semantically correct?
What it does:
- Gets last 3 rows from database
- Fetches user input from a web form
- Inserts (some) user input into database
- Takes (remaining) user input to calculate a sum
- Updates database row and sets the empty/null columns
function secondsToTime($seconds) {
$dtF = new DateTime("@0");
$dtT = new DateTime();
$dtT->setTimestamp($seconds);
return $dtF->diff($dtT)->format('%a days, %h hours and %i minutes');
}
function secondsToDay($days) {
$dtF = new DateTime('@0');
$dtT = new DateTime();
$dtT->setTimestamp($days);
return $dtF->diff($dtT)->format('%a');
}
if (isset($_GET['submitPeriod'])) {
$resultAvg = mysqli_query($conn, "SELECT sum(elapsed) FROM (SELECT elapsed FROM periods ORDER BY id DESC LIMIT 0, 3) AS elapsedNew"); //select last 3 rows
$estimateNew = mysqli_fetch_assoc($resultAvg)['sum(elapsed)']/2;
$d_o = date_format(date_create($_GET['d_o']), "Y-m-d H:i:s");
$d_i = date_format(date_create($_GET['d_i']), "Y-m-d H:i:s");
$n_e = secondsToDay($estimateNew);
$notes = $_GET['notes'];
//Calculate duration of period
$datetime1 = new DateTime($d_o);
$datetime2 = new DateTime($d_i);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%a days %h hours %i minutes');
//Calculate next estimated day depending on value provided
$estimatedNumber = $n_e;
$estimatedDate = new DateTime($d_i);
$estimatedDate->modify('+'.$estimatedNumber.' days');
$estimate = $estimatedDate->format('Y-m-d H:i:s');
mysqli_query($conn, "
INSERT INTO periods
VALUES (null, null, '$d_o', '$d_i', '$elapsed', '$estimate', '$n_e days', '$notes')
");
//Calculate duration since last period
$getElapsed = mysqli_query($conn, "
SELECT TIMESTAMPDIFF(second, (SELECT d_i FROM periods ORDER BY ID DESC LIMIT 1, 1), (SELECT d_o FROM periods ORDER BY ID DESC LIMIT 1)) AS elapsedTime
FROM periods LIMIT 1");
$elapsedResult = mysqli_fetch_assoc($getElapsed)['elapsedTime'];
mysqli_query($conn, "
UPDATE periods
SET elapsed='$elapsedResult'
WHERE id=(SELECT id ORDER BY ID DESC LIMIT 1)
ORDER BY ID DESC LIMIT 1
");
header('Location: ?');
}
I am using 3 stages (insert, select, update) to complete the program before the page is updated.
What I’d like to know is if the code is written badly, and what should be done to improve it. Such advice would be invaluable to me.
Also, if this is not the place to discuss it, please advise where I could receive input.