bsamson Posted January 19, 2008 Share Posted January 19, 2008 Hello ... I currently have a date field in my MySQL DB and ofcourse that's stored in this format: 2008-01-19 12:32:50 The format the end user enters the date is: 01/19/2008 which is stored in $reqdate (i.e.: mm/dd/yyyy) I currently have this MySQL query ... <?php $query = mysql_query("SELECT * FROM nightly WHERE entrydate='$reqdate'"); ?> Obviously this doesn't work. How do I rewrite the above query to check the end user date against the mysql formated date? Any help will be very much appreciated! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/ Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 May be an easier way, but you could explode it on "/" then just take the 3 values and put them in any order you want. Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443597 Share on other sites More sharing options...
$username Posted January 19, 2008 Share Posted January 19, 2008 I dont use the Data/time format for MYSQL. It is easier to have the field a varchar and then just setup your PHP code to enter the date that you would like. you can try doing a ereg_replace("[/]", "-") on your 01/19/2008 then just add the PHP time to make it work code example $time = ereg_replace("[/]", "-", $reqdate); $mysql-time = $time, and your HH:MM:SS Brett Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443609 Share on other sites More sharing options...
bsamson Posted January 19, 2008 Author Share Posted January 19, 2008 Thanks for everyones help. I just wish there was an easier way ... like a built in function to check only the date. Oh well, thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443631 Share on other sites More sharing options...
revraz Posted January 19, 2008 Share Posted January 19, 2008 The problem is you are allowing a date format that you can't do much with. So what you should do is allow them to use that date format for entering it, but you convert it after and store it correctly in the DB. There is no function, unless you make one, that does this, because it doesnt know if you use dd/mm/yy mm/dd/yy etc Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443636 Share on other sites More sharing options...
bsamson Posted January 19, 2008 Author Share Posted January 19, 2008 Ok one more question. Let's say I make the user (via a form) enter the date the way MYSQL datetime field. So let's say I want to query the DB to match the date 2008/01/19 ... how to I write that to just search for the date? So basically disregard the entered time? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443640 Share on other sites More sharing options...
bsamson Posted January 19, 2008 Author Share Posted January 19, 2008 For Example ... I tried this but it doesn't seem to work: <?php $reqdate = "2008/01/19"; $query = mysql_query("SELECT * FROM nightly WHERE date('Y/m/d',strtotime(entrydate)=$reqdate"); ?> Any Suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443649 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2008 Share Posted January 19, 2008 Use the mysql STR_TO_DATE() fucntion in your query to convert any format data into a mysql DATE type - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443658 Share on other sites More sharing options...
bsamson Posted January 19, 2008 Author Share Posted January 19, 2008 Actually I am trying this: <?php $query = mysql_query("SELECT * FROM nightly WHERE DATE_FORMAT(entrydate, '%c/%d/%Y')=$reqdate"); ?> Now, just trying to get it working Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443662 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2008 Share Posted January 19, 2008 Use the mysql DATE() function to only use the date part of your DATETIME type in a comparison - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date Your query in your last previous post won't work because php code does not execute inside of strings. Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443663 Share on other sites More sharing options...
bsamson Posted January 19, 2008 Author Share Posted January 19, 2008 I am a little confused. Are you suggesting that I do this: <?php $query = mysql_query("SELECT * FROM nightly WHERE DATE(entrydate)=$reqdate"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443667 Share on other sites More sharing options...
bsamson Posted January 19, 2008 Author Share Posted January 19, 2008 Unfortunetly the above Query doesn't work. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/86787-how-to-convert-user-entered-date-to-mysql-date-to-compare/#findComment-443676 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.