I’m sorry for the terribly long question. I just want to make sure that this problem is fully addressed:
so through an html form, I asked user to give four information five times: deliveryby, itemNo, inputTime, inputTime2.
Each of this information is stored inside a session array.
All of this 5 items will be displayed and for each of the item, user can either click on ‘retrieve’ button or ‘delete’ button.
so for each of these 5 items, I need a function that can track the number of days between each inputTime2 and inputTime.
if the days is more than 2 days, ‘delete’ button can be clicked and the item will be cleared from session.
but if it’s less than 2 days, ‘retrieve’ button can be clicked and the item will also be cleared from session.
Both retrieve and delete buttons will delete the items from session.
the only difference is the day’s duration that both buttons need to scan.
Box-function.php : where the date function is. this page is attached to the ‘delete’ button. I’m not sure whether I’m doing it right.
<?php session_start(); $x = $_GET['id']; //echo $x; $date1 = array(); $date2 = array(); $datediff = array(); $days = array(); //to assign the session date array and find out how many days are between the two dates for($x=0; $x<5; $x++){ $date1 = [strtotime($_SESSION['InputTime'][$x])][$x]; $date2 = [strtotime($_SESSION['InputTime2'][$x])][$x]; $datediff = [$date2 - $date1][$x]; $days = [round($datediff / (60*60*24))][$x]; } for($x=0; $x<5; $x++){ if ($days[$x] == 2 && $days[$x] > 2){ //if( date("Y-m-d", strtotime("+2 day", strtotime($_SESSION['InputTime'][$x])))){ $_SESSION['DeliveryBy'][$x] = ''; $_SESSION['ItemNo'][$x] = ''; $_SESSION['InputTime'][$x] = ''; header('Location: User-display1.php'); } } ?>>
User-display1.php:
for($x=0; $x<5; $x++) { if($_SESSION['DeliveryBy'][$x]!=""){ echo "<tr>"; echo "<td>"; echo $x+1; echo "</td>"; echo "<td>"; echo $_SESSION['DeliveryBy'][$x]; echo "</td>"; echo "<td>"; echo $_SESSION['InputTime'][$x]; echo "</td>"; echo "<td>"; ?> <a href="clear-session.php?id=<?php echo $x; ?>" > <button type="button" class="btn btn-success">retrieve</button></a> <?php echo "</td>"; echo "<td>"; ?> <a href="Box-function.php?id=<?php echo $row["id"]; ?>"><button type="button" class="btn btn-danger">Delete</button></a> <?php echo "</td>"; echo "</tr>"; } else{ echo "<tr>"; echo "<td>"; echo $x+1; echo "</td>"; echo "<td>"; echo ' '; echo "</td>"; echo "<td>"; echo ' '; echo "</td>"; echo "<td>"; ?> <a href="clear-session.php?id=<?php echo $x; ?>" > <button type="button" class="btn btn-success">retrieve</button></a> <?php echo "</td>"; echo "<td>"; ?> <a href="Box-function.php?id=<?php echo $row["id"]; ?>"><button type="button" class="btn btn-danger">Delete</button></a> <?php echo "</td>"; echo "</tr>"; } }
clear-session.php : this page is supposed to clear session variables for items with days difference of less than 2 days but I’m not sure how to do it since I’m still having trouble with the Box-function.php page.
<?php session_start(); $x = $_GET['id']; //echo $x; $_SESSION['DeliveryBy'][$x] = ''; $_SESSION['ItemNo'][$x] = ''; $_SESSION['InputTime'][$x] = ''; header('Location: User-display1.php'); ?>
the code shows no error. but I cannot click on the delete button. there were no changes when the button was clicked. I think there might be some mistakes in the logic of this code. Can anyone advise me on what I should do?
Advertisement
Answer
Well, you didnt really specify what format the dates are in. But I do see this:
strtotime($_SESSION['InputTime'][$x])
So I assume its at least something that PHP understands. Knowing that, you can use something like this:
<?php $old_o = new DateTime('2019-12-31'); $new_o = new DateTime; echo $new_o->diff($old_o)->days, "n";