Jump to content

help with date input


desjardins2010

Recommended Posts

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.

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

 

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 by Psycho
Link to comment
Share on other sites

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 by desjardins2010
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.. 

Link to comment
Share on other sites

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.

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.