Jump to content

How do I compare dates that are not dates without the minutes


blue-genie

Recommended Posts

i have 2 fields that are stored as varchars that are going into the db like this

 

2010-05-17 00:00:00

 

now i'm trying to do a select statement that says bring me back anything that is valid today.

 

so if startDate is 2010-05-17 00:00:00

and endDate is 2010-05-17 00:00:00

 

and now() is 2010-05-17 then it should be okay (not looking at time

 

$result = mysql_query("SELECT* from gamedetails where Date('startDate') < now() and Date('endDate') >= now() and gameIsActive = '1'") or die ('Error: '.mysql_error ());

 

 

Link to comment
Share on other sites

i have 2 fields that are stored as varchars that are going into the db like this

 

2010-05-17 00:00:00

 

now i'm trying to do a select statement that says bring me back anything that is valid today.

 

so if startDate is 2010-05-17 00:00:00

and endDate is 2010-05-17 00:00:00

 

and now() is 2010-05-17 then it should be okay (not looking at time

 

$result = mysql_query("SELECT* from gamedetails where Date('startDate') < now() and Date('endDate') >= now() and gameIsActive = '1'") or die ('Error: '.mysql_error ());

 

Use date():

 

if date(startDate) = date('2010-05-17') or date(now) = '2010-05-17'

 

etc.  That strips the time off of a date and returns just YYYY-MM-DD

Link to comment
Share on other sites

i don't know how to do databases properly and i had to put this together myself from scratch (i'm a flash developer) - so  I did what was the easiest for me at the time.

I'm trying to not change it at this point because i'm sure there will be alot of repercussions.

Link to comment
Share on other sites

There are likely to be more repercussions if you don't change it. You already store it in MySQL compatible format, so it is likely that no changes in application code will be necessary. All you need to do is to change datatype from VARCHAR to DATETIME. This will make all operations on dates much (MUCH) easier.

Link to comment
Share on other sites

i've decided to do that. (do things properly)

have changed it to Date (not dateTime) and i've done this now which works but i'm sure is messy and laughable.

 

$today = getdate();

$y = $today[year];

$m = $today[mon];

$d = $today[mday];

$compDateStr = "".$y."-".$m."-".$d."";

 

$result = mysql_query("SELECT* from gamedetails where startDate <= date('".$compDateStr."') AND endDate >= date('".$compDateStr."') AND gameIsActive = 1");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.