I'm working on an auction script for a client. This portion of the script pulls all the open auctions from the database and allows bidding, but they only want bidding on auction with 30 seconds left until it ends.

$query = "select * from DSI_auctions
         WHERE closed='0' AND
         suspended='0' AND ";
         $query .= "starts<=".$NOW." 
         order by ends LIMIT ".$SETTINGS['endingsoonnumber'];
$result = mysql_query($query);


$num_auction = mysql_num_rows($result);
    //echo $num_auction;

I've thought of having the script change the current time and ending time to epoch and determinig the difference but if I do that it will only return 1 row even if I add something like:

$AE = 0;
while $AE < 10 {


}
$AE++

I am stupmed.

put $AE++ inside the loop.

Yeah, sorry thats my bad... But the query still only pulls auctions that have started

$query .= "starts<=".$NOW."

. I need it to pull auctions that end in 30 secs or less...

Member Avatar for diafol
//set timezone - ensure DB TZ same as script TZ??
$now = date('Y-m-d H:i:s);
$end = date('Y-m-d H:i:s, time() + 31);
//I'm assuming your datetimes are stored in yyyy-mm-dd hh:mm:ss format
$query = "select * from DSI_auctions WHERE closed='0' AND suspended='0' AND starts <= '$now' AND ends < '$ends' ORDER BY ends LIMIT ".$SETTINGS['endingsoonnumber'];
$result = mysql_query($query);

Something like that?

You can have a javascript timer then to countdown to the end of each one (no need to have multiple timers - just one to decrement each value - disable on 0)

Thank you so much, I think that's it and I can NOT believe I didn't think of that... My brain hurts.... it's been a long week for sure....

I'll try that when I get back in.

This is what I used. Thank you so much for the insight.

$endsoon = ($NOW  + 55);
$query = "select * from DSI_auctions
         WHERE closed='0' AND
         suspended='0' AND ";
         $query .= "starts<=".$NOW." AND ";
		 $query .= "ends<".$endsoon."
         order by ends LIMIT ".$SETTINGS['endingsoonnumber'];
$result = mysql_query($query);

I increased it to 55 as this cron runs every minute and loops 60 times grabbing other variables. Having it at 31 caused a delay and would not allow bidding at 30 secs.

Just a note, instead of using the $NOW variable in your query, you can also use the NOW() function of MySQL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.