desjardins2010 Posted March 11, 2014 Share Posted March 11, 2014 i have this code http://pastebin.com/XBJxGw6L prob is when using the input type=date when selecting the date it's showing as m-d-y but it's written to the database as Y-m-d so my query is failing to compare dates as it's checking in the format i believe m-d-y ?? Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/ Share on other sites More sharing options...
Psycho Posted March 11, 2014 Share Posted March 11, 2014 Your link is broken or the content has been removed. You need to modify the date input from the user so it is in the proper format for using within a database query. Databases store the date in the format YYYY-MM-DD for several reasons. For example, it is agnostic to varying formats by regions. In the US we use MM-DD-YYYY, but in many other countries they use DD-MM-YYYY. Do not try to store dates as you read them, then you cannot use the data functions within your database. Just jchange the format before you use it. Look into strtotime() and date() functions. Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472146 Share on other sites More sharing options...
desjardins2010 Posted March 11, 2014 Author Share Posted March 11, 2014 <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $siteName = $_POST['sitename']; $siteName = trim(htmlentities($siteName)); $dest_url = $_POST['url']; $dest_url = trim(htmlentities($dest_url)); $start_date = $_POST['startdate']; $start_date = trim(htmlentities($start_date)); $end_date = $_POST['enddate']; $end_date = trim(htmlentities($end_date)); $imageurl = $_POST['imageurl']; $imageurl = trim(htmlentities($imageurl)); $insert = "INSERT INTO `adverts` (siteName,dest_url,start_date,end_date,imageurl) VALUES ('$siteName','$dest_url','$start_date','$end_date','$imageurl')"; $insertquery = $mydb->query($insert); if ($insertquery) { $completed = true; } //end first if } ?> <div class="admin_wrap_rhs_form"> <table> <b class="alert_t1"> <?php if ($completed == true) { echo "New advertisement added successfully"; } ?></b> <form action="" method="post" name="addsite" onsubmit="return (validatecontact());"> <tr><td>Site Name</td><td>:</td><td><input type="text" name="sitename" class="admin_inp1" placeholder="SiteName"></td></tr> <tr><td>Destination URL:</td><td>:</td><td><input type="text" name="url" class="admin_inp1" placeholder=" Destination URL"></td></tr> <tr><td>Start Date:</td><td>:</td><td><input type="date" name="startdate" class="admin_inp1" placeholder="01-01-2014"></td></tr> <tr><td>End Date:</td><td>:</td><td><input type="date" name="enddate" class="admin_inp1" placeholder="01-29-2014"></td></tr> <tr><td>100*100 Image URL:</td><td>:</td><td><input type="text" name="imageurl" class="admin_inp1" placeholder="http://www.domain.com/images/banner.gif"></td></tr> <tr><td colspan="3"><center><input type="reset" value="Reset" class="admin_but"> <input type="submit" value="Add Site" class="admin_but"></center></td></tr> </form> </table> <p style="margin-top: 10px; ">NOTICE: remember you can use your image hosting here for your banner ads also <a href="index.php?action=upload_img">HERE</a></p> </div> this is the code the data being supplied by userend is directly from the input date function -- it's storing in DB as 2014-03-11 but my compare is looking to compare m-d-y now when you select a date using the form input its in the m-d-y so am I doing something to the entry when trim(htmlentities()) to the string? Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472147 Share on other sites More sharing options...
Psycho Posted March 11, 2014 Share Posted March 11, 2014 (edited) this is the code the data being supplied by userend is directly from the input date function -- it's storing in DB as 2014-03-11 but my compare is looking to compare m-d-y now when you select a date using the form input its in the m-d-y so am I doing something to the entry when trim(htmlentities()) to the string? Why did you feel the need to restate your question which I just answered? You need to convert the date from the user entered format to the appropriate format for the database. Databases use YYYY-MM-DD. You need to convert the format from DB to user presentation (and Vice versa) as needed in your code. I don't see any compare logic in the above code, so I think your confusion may be that the DB is automatically converting the dates for you - which you should not rely on. You should be converting the dates before inserting into your database. Edited March 11, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472151 Share on other sites More sharing options...
desjardins2010 Posted March 11, 2014 Author Share Posted March 11, 2014 (edited) i felt the need to reply cause I'm a little confused hence the need for a help forum - where i'm getting lost is and mentioned above the format that the input type=date TAKES is m-d-y <-- this is how I want it to stay - when you process the form it gets stored in Y-m-d my question is I'm storing to DB in varchar is that wrong? and if thats the case how do I convert from Y-m-d to m-d-y Edited March 11, 2014 by desjardins2010 Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472152 Share on other sites More sharing options...
Psycho Posted March 11, 2014 Share Posted March 11, 2014 my question is I'm storing to DB in varchar is that wrong? Yes, that is wrong. I You should use the database DATE type - and it will be stored in the format YYYY-MM-DD and you should not try to store it in a different format. You should convert the data formats between YYYY-MM-DD to whatever format you use for user output and back again. If you do not store the values as a DATE type you will be unable to use the built-in date functions of the database. . . . and if thats the case how do I convert from Y-m-d to m-d-y Already answered above Look into strtotime() and date() functions. Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472155 Share on other sites More sharing options...
desjardins2010 Posted March 11, 2014 Author Share Posted March 11, 2014 Ok I understand what your saying I just don't understand how you take an entry from a form in this case $_POST['startdate'] that is carrying the value 2014-03-11 and have it changed to 03-11-2014 to store? I'm know strtotime() and date() but not sure how you would use them to alter the date format.. Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472158 Share on other sites More sharing options...
Psycho Posted March 11, 2014 Share Posted March 11, 2014 I'm know strtotime() and date() but not sure how you would use them to alter the date format.. Forgive me if I sound rude, but I cannot believe that statement. I can't understand how you would know how to use those functions yet not know how to use them to change the format of a date. The only possibilities I can think of are 1) you have no clue what you are talking about, 2) you have an idea but don't have the knowledge or initiative to do it yourself and are just wanting someone to do it for you, or 3) you're trolling. Let's break the problem down. You have a date such as 12-2-2013 and need to convert it to 2013-12-02. I already gave you two functions that you could use to solve this, right? You'll need to start with the original value of "12-2-2013". On which of those two functions could you use such a value? That will tell you which one to use first. Then, think about the output you would get with that function and how you could use it in the second function to get the result you want. Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472161 Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 I'd also point out that m-d-y is a terrible way to store dates. That's a "carry-over" from the "business" world that has caused no end to havoc and headache in the computer world. Quote Link to comment https://forums.phpfreaks.com/topic/286881-help-with-date-input/#findComment-1472164 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.