Jump to content

Recommended Posts

Guys, can someone give me a hint on this.

 

Currently i have 2 columns in db to store the date but the structure is in varchar

 

How can I compare the date with these columns because I did try to use str to date function but now working.

 

My script as below.

my column name is join and grad and data store in these varchar columns format is like this : 14/03/2001 11:30:00

 

$now = date("Y-m-d");

$beu_main = mysql_query("SELECT * FROM `students` WHERE STR_TO_DATE( join, '%d/%m/%Y' )<='$now' AND STR_TO_DATE( grad, '%d/%m/%Y' ) > '$now'");

 

 

 

anything wrong with my query or any quick fix? thankyou.

Link to comment
https://forums.phpfreaks.com/topic/269213-comparing-date/
Share on other sites

Apart from your using varchar for dates (and a useless format) instead of datetime (YYYY-MM-DD HH:ii:ss), and instead of '$now' you could use CURDATE().

 

I cannot see any obvious errors. Define "not working" and check your data.

Link to comment
https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383647
Share on other sites

thanks guys

 

but how can i sort this column by showing the latest date first since this is a varchar column?

 

i tried

$re= mysql_query("SELECT * FROM `students` ORDER BY STR_TO_DATE( join, '%d/%m/%Y' ) DESC");

 

but it is not working.. can someone pls assist..thanks

Link to comment
https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383888
Share on other sites

Sorry for not spotting this earlier.

 

Your date column name "join" is a reserved word in SQL. You should either rename it (recommended) to something like join_date or enclose the name in backticks in your queries eg

STR_TO_DATE(`join`, '%d/%m/%Y')

 

If you check the value of mysql_error() when you get problems you should get there a little faster.

Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383895
Share on other sites

I set up a test table and this looks OK

mysql> SELECT * FROM students;
+-----+----------+---------------------+
| pid | username | join			    |
+-----+----------+---------------------+
|   1 | MMDE1    | 01/11/2012 09:30:55 |
|   2 | MMDE2    | 05/10/2012 08:55:00 |
|   3 | MMDE3    | 08/10/2012 12:25:00 |
|   4 | MMDE4    | 06/10/2012 15:30:00 |
+-----+----------+---------------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM students ORDER BY STR_TO_DATE(`join`, '%d/%m/%Y') DESC;
+-----+----------+---------------------+
| pid | username | join			    |
+-----+----------+---------------------+
|   1 | MMDE1    | 01/11/2012 09:30:55 |
|   3 | MMDE3    | 08/10/2012 12:25:00 |
|   4 | MMDE4    | 06/10/2012 15:30:00 |
|   2 | MMDE2    | 05/10/2012 08:55:00 |
+-----+----------+---------------------+
4 rows in set, 4 warnings (0.00 sec)

Link to comment
https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383900
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.