-1

How would i go about checking to see if an auction has expired in my database? I have a datetime column in MySQL that i believe is of the following format: YYY-MM-DD hh:mm:ss. If this is the case would the following check work - i.e. want to select only expired auctions from the table in the database...

<?php

//Some code    

$auctioncheck = mysql_query("

 SELECT * FROM auction WHERE ($date_time > finish_time)

");

?>

While "finish time" is a column in the database of the above cited format. Presuming this works how actually do i get the current date into the same format? If anybody knows i would very grateful cheers. Even more so if the above query wouldn't work and something else is required. Thanks again.

Oh and of course i would define the date_time variable to start with

Roy
  • 49
  • 6

4 Answers4

2

Do you actually need the $date_time variable? The easiest way to do this would be SELECT * FROM auction WHERE finish_time < NOW(). That way you'll get your results and don't have to set the date from PHP.

danaketh
  • 148
  • 1
  • 6
1

You can use

<?php

//Some code    
$date_time=strtotime($date_time);
$auctioncheck = mysql_query("

SELECT * FROM auction WHERE ( $date_time > UNIX_TIMESTAMP(finish_time))

");

?>
user1259132
  • 320
  • 2
  • 3
  • 11
0

This solution:

// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');

// or, to show date only
$date->get('YYYY-MM-dd') 

is from this post: PHP Zend date format

and here's another solution: how to format a Date in MM/dd/yyyy HH:mm:ss format in javascript?

and to do it in php: How to get the current date and time in PHP?

Community
  • 1
  • 1
0

The function date is made for it:

// Get the current date
$date_time = date("y-m-d H:i:s");

Look at the documentation page to see other format flags ;)

Nicolas Dusart
  • 1,867
  • 18
  • 26
  • I've put it to use but the hour is one displayed is one hour ahead. What is likely responsible for the discrepancy? – Roy Dec 12 '12 at 11:29
  • this give you the time in the timezone of your server. Maybe your localtime is different. Please be aware that if you compare times, you have to compare times in the same timezone. I advice to use the timezone of your database server. – Nicolas Dusart Dec 12 '12 at 11:32
  • This is a common question with common answers, at least I have been honest enough to refer the person who asked to other posts in stack overflow unlike pretending to be some "guru" when if I were to look closely, I would find that you have simply copied and pasted. Why try so hard to derail other people's efforts? – iOSAndroidWindowsMobileAppsDev Dec 12 '12 at 12:41
  • I'm not the one who urge the poster to accept my answer, and that keep editing the answer to just englobe all the others. And sorry to disappoint you, but I understand quite the easy format string syntax for dates, so no, I didn't need to copy/paste anything ;) – Nicolas Dusart Dec 12 '12 at 14:37