Reputation: 93
I'm pretty new to PHP. I started a website with products bidding for a project and i'm stuck on this thing.. I want to store the actual date and time when a new product is added to the page and another date and time (deadline) witch is 24 hours later. I've done this in the adauga_admin.php page (the page that adds products details to mysql table)
adauga_admin.php :
<?php
include('connect.php');
$data=date('Y-m-d H:i:s');
$deadline=date('Y-m-d H:i:s',strtotime('+24 hours'));
// protejam impotriva injectiei de cod
$t = mysqli_real_escape_string($con, $_POST['t']);
$s = mysqli_real_escape_string($con, $_POST['s']);
$p = mysqli_real_escape_string($con, $_POST['p']);
$d = mysqli_real_escape_string($con, $_POST['d']);
$sql="INSERT INTO produse (titlu, stare, pret, descriere, data, deadline)
VALUES ('$t', '$s', '$p', '$d', '$data', '$deadline')";
$rez = mysqli_query($con,$sql);
if($rez) {
header("location:produse_user.php");
}
else {
echo "Probleme la adaugare";
}
mysqli_close($con);
?>
This is the table with the products :
id | titlu | stare | pret | descriere | data | deadline |
___________________________________________________________________________________
11 | asd | asd | 140 | asd | 2014-06-03 19:02:47 | 2014-06-04 19:02:47 |
And this is where I display the products to the users. From here they can bid for a product
produse_user.php :
<?php
include('connect.php');
?>
<?php
$result = mysqli_query($con,"select * from produse");
while ($row = mysqli_fetch_array($result))
{
?>
<div class="produse">
<div class="produse-header">
<h4><?php echo $row['titlu'];?></h4>
<h5><?php echo $row['stare'];?></h5>
</div>
<div class="produse-continut">
<textarea readonly><?php echo $row['descriere'];?></textarea>
</div>
<div class="produse-pret">
<p><?php echo $row['pret'];?> lei</p>
</div>
<div class="produse-buton">
<a href="liciteaza.php?id=<?php echo $row['id']; ?>">Liciteaza</a>
</div></div>
<?php
}
mysqli_close($con);
?>
My goal is to make a script that displays on the poducts page for each product if is still available or expired. I guess it is something like this but and don't know how to do it..
if ($data > $deadline) {
echo "Expired";
}
else {
echo "Available";
}
Upvotes: 0
Views: 75
Reputation: 3382
Try this:
<?php
include('connect.php');
?>
<?php
$data = date('Y-m-d H:i:s');
$status = "";
$result = mysqli_query($con,"select * from produse");
while ($row = mysqli_fetch_array($result))
{
if ($data > $row['deadline']) {
$status = "Expired";
} else {
$status = "Available";
}
?>
<div class="produse">
<div class="produse-header">
<h4><?php echo $row['titlu'];?></h4>
<h5><?php echo $row['stare'];?></h5>
<h5><?php echo $status;?></h5>
</div>
<div class="produse-continut">
<textarea readonly><?php echo $row['descriere'];?></textarea>
</div>
<div class="produse-pret">
<p><?php echo $row['pret'];?> lei</p>
</div>
<div class="produse-buton">
<a href="liciteaza.php?id=<?php echo $row['id']; ?>">Liciteaza</a>
</div></div>
<?php
}
mysqli_close($con);
?>
Upvotes: 0
Reputation: 108841
You can use the following query or a variant on it.
select * from produse WHERE deadline >= NOW()
to exclude rows for which the deadline has already passed from your result set.
You can also do stuff like
select * from produse WHERE deadline >= NOW() - INTERVAL 2 HOUR
AND deadline < NOW()
to show products with deadlines between two hours ago and the present time.
Upvotes: 2