-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

4
  • can paste value of $date_time ?
    – GBD
    Dec 12, 2012 at 11:19
  • sorry i haven't even tested it yet. For i was unsure of how to generate the $date_time (current time) to start with. Any idea?
    – Roy
    Dec 12, 2012 at 11:20
  • There are plenty of similar questions on SO.
    – Salman A
    Dec 12, 2012 at 11:26
  • HS : Don't forget to securize your request by using the mysqli_real_escape_string() on each variable included in a mysql request to prevent mysql injection Dec 12, 2012 at 11:28

4 Answers 4

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.

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))

");

?>
1
  • I hope this will work for any date format we send to the query Dec 12, 2012 at 11:38
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?

9
  • Thanks a lot i'll see if the query works on this basis. Do you have hunch? By that i mean can you ordinarily just see which two dates is the "greater" (more recent) by using ">".
    – Roy
    Dec 12, 2012 at 11:22
  • Next to your greater than sign, you would need the comparison to be in the same format. stackoverflow.com/questions/6370538/…. IF MY SOLUTION WORKS FOR YOU, PLEASE ACCEPT. Dec 12, 2012 at 11:26
  • unneccessary to use the Zend framework to just get a date, the native functions of PHP are just enough ;) Dec 12, 2012 at 11:28
  • I gave another alternative :) Dec 12, 2012 at 11:29
  • Do you know why the time is given an hour ahead? Time zones surely. What would i need to tinker with?
    – Roy
    Dec 12, 2012 at 11:33
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 ;)

4
  • 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, 2012 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. Dec 12, 2012 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? Dec 12, 2012 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 ;) Dec 12, 2012 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.