Jump to content

How to convert user entered date to MySQL date to compare ...


Recommended Posts

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!

 

 

 

 

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

 

 

 

 

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

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.

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

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.

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.