<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[KillerSites.com Web Design Forums - Changing date format from form to database]]></title>
		<link>http://www.killersites.com/forums/topic/2400/changing-date-format-from-form-to-database/</link>
		<description><![CDATA[The most recent posts in Changing date format from form to database.]]></description>
		<lastBuildDate>Wed, 04 Nov 2009 10:07:53 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Changing date format from form to database]]></title>
			<link>http://www.killersites.com/forums/post/11847/#p11847</link>
			<description><![CDATA[<p>I got there in the end.<br />I used <br />&lt;input id=&quot;date&quot; type=&#039;text&#039; name=&#039;date10&#039; value=&#039;&lt;?php echo date(&quot;d F Y&quot;, strtotime(&#039;+10 days&#039;));?&gt;&#039; size=&#039;30&#039;&gt; <br />in the form code to show the future date in the full format 14 November 2009 (today + 10 days)</p><p>The name=&quot;date10&quot; in the form creates a variable $date10 <br />$date10 = $_POST[&#039;date10&#039;];</p><p>and then I created a new variable taking the string from date10 in the full format and converting it into a y-m-d format <br />$date = date(&#039;y-m-d&#039;, strtotime(&quot;$date10&quot;));</p><p>which gets fed to the database<br />$result=MYSQL_QUERY(&quot;INSERT INTO my-table (id, place, date, time)&quot;.<br />&quot;VALUES (&#039;NULL&#039;, &#039;$place&#039;, &#039;$date&#039;, &#039;$time&#039;)&quot;) or die ( &quot;&lt;p&gt;&lt;span style=\&quot;color: red;\&quot;&gt;Unable to select table&lt;/span&gt;&lt;/p&gt;&quot;);</p><p>so the form&#039;s proposed future date can now be edited by a viewer and will go into the database (unless he edits it in an incorrect way, of course, but then I have the timestamp in the database as a check).</p>]]></description>
			<author><![CDATA[dummy@example.com (Wickham)]]></author>
			<pubDate>Wed, 04 Nov 2009 10:07:53 +0000</pubDate>
			<guid>http://www.killersites.com/forums/post/11847/#p11847</guid>
		</item>
		<item>
			<title><![CDATA[Re: Changing date format from form to database]]></title>
			<link>http://www.killersites.com/forums/post/11819/#p11819</link>
			<description><![CDATA[<p>Thanks for your reply; I haven&#039;t had time to try your codes yet; I took the easy way out temporarily by having one bit of PHP for the form input value to calculate the current date plus 10 days in d F Y format so that it shows as 13 November 2009 in the form and then a variable for the MySQL submission which has the same calculation but in the format y-m-d which MySQL can accept.</p><p>&lt;input id=&quot;date&quot; type=&#039;text&#039; name=&#039;date10&#039; value=&#039;&lt;?php echo date(&quot;d F Y&quot;, strtotime(&#039;+10 days&#039;));?&gt;&#039; size=&#039;30&#039;&gt;</p><p>and for the MySQL link (part shown):-</p><p>$date =&nbsp; date(&quot;y-m-d&quot;, strtotime(&#039;+10 days&#039;));</p><p>$result=MYSQL_QUERY(&quot;INSERT INTO my-table (id, place, date, time)&quot;.<br />&quot;VALUES (&#039;NULL&#039;, &#039;$place&#039;, &#039;$date&#039;, &#039;$time&#039;)&quot;) or die ( &quot;&lt;p&gt;&lt;span style=\&quot;color: red;\&quot;&gt;Unable to select table&lt;/span&gt;&lt;/p&gt;&quot;);</p><p>The form and the database show the same date (in different formats) but it means that if someone edits the date in the form, the value=&quot;...&quot; in the form isn&#039;t taken to the database, it&#039;s the other variable which I haven&#039;t been able to link to the form value yet. </p><p>I assume that as the form value is echo date() it must be a string, so what I need is a function to convert a string into a different format that I can feed outside the form to a new variable that I can send to MySQL. Every time I change the format inside the form it changes the format of the form display which I don&#039;t want.</p>]]></description>
			<author><![CDATA[dummy@example.com (Wickham)]]></author>
			<pubDate>Tue, 03 Nov 2009 19:52:08 +0000</pubDate>
			<guid>http://www.killersites.com/forums/post/11819/#p11819</guid>
		</item>
		<item>
			<title><![CDATA[Re: Changing date format from form to database]]></title>
			<link>http://www.killersites.com/forums/post/11806/#p11806</link>
			<description><![CDATA[<div class="quotebox"><blockquote><p>How can I change the format inside the input tag so that the date is sent to the database in a different format but still keep the form in the format I want?</p></blockquote></div><p>This is something I&#039;ve had trouble with in the past...&nbsp; I think the solution here is to create two translation functions that convert to and from the human readable format you want to display within the &lt;input&gt; with the timestamp format (year-month-day) that you want to save it to the database in.</p><p>Here&#039;s one example of what I&#039;ve done:</p><p>All my dates are stored in the database as year-month-day-time (for example, &quot;2008-12-12 00:00:00&quot;). If the user enters a date in a different format &quot;month/day/year&quot; for example, it gets converted before being saved into the database.</p><p>I then have two functions that convert to and from that format (which I&#039;ve called &quot;timestamp&quot;) and a more usable format (&quot;month/day/year&quot;).</p><div class="codebox"><pre><code>// convert current time to timestamp
// input: none
// returns now() in timestamp (0000-00-00 00:00:00) format
function getTime()
{
    return date(&quot;Y-m-d H:i:s&quot;, time());
} 
    
// converts date (00/00/00) to timestamp
// input: date, delimiting character(s)
// output: string in timestamp format
function convertDateToTimestamp($date, $delimiter)
{
    $date = explode($delimiter, $date);
    return &#039;20&#039;.$date[2].&#039;-&#039;.$date[0].&#039;-&#039;.$date[1].&#039; 00:00:00&#039;;
}
    
// convert timestamp to date (00/00/00)
// input: timestamp, delimiter
// output: string in date format
function convertTimestampToDate($timestamp, $delimiter)
{
    $date = substr($timestamp, 2, 8);
    $date = explode(&quot;-&quot;, $date);
    return $date[1] . $delimiter . $date[2] . $delimiter . $date[0];
}</code></pre></div>]]></description>
			<author><![CDATA[dummy@example.com (falkencreative)]]></author>
			<pubDate>Tue, 03 Nov 2009 08:11:42 +0000</pubDate>
			<guid>http://www.killersites.com/forums/post/11806/#p11806</guid>
		</item>
		<item>
			<title><![CDATA[Changing date format from form to database]]></title>
			<link>http://www.killersites.com/forums/post/11797/#p11797</link>
			<description><![CDATA[<p>This is an input tag from a simple form to show a proposed future delivery date:-<br />&lt;input id=&quot;date&quot; type=&#039;text&#039; name=&#039;date10&#039; value=&#039;&lt;?php echo date(&quot;d F Y&quot;, strtotime(&#039;+10 days&#039;));?&gt;&#039; size=&#039;30&#039;&gt;</p><p>which shows in the form input box as 12 November 2009 (+10 days from current date) and is in the format I want to display.</p><p>When submitted to the MySQL database the date is rejected because the database can only accept a date in a format of y-m-d or y/m/d.</p><p>How can I change the format inside the input tag so that the date is sent to the database in a different format but still keep the form in the format I want? </p><p>I can code the input tag in the correct format for the database but then the display is not in the format I want.</p><p>It seems that the value=&quot;...&quot; in the form input tag serves two purposes, to display the value in the form and to send the value to the database, so if I format for the display it sends the wrong format to the database; if I try to put two values in the input tag it objects or sends the wrong one to the database. I have thought of having a dummy input tag with display: hidden but can&#039;t get it to work yet.</p><p>Additionally, if the format is y-m-d (which works but I don&#039;t want) the viewer can change the date if he doesn&#039;t like it and the revised date will go to the database, and I&#039;d like this to happen with my desired format. I may have to put the date outside the form, but then how do I get it into the database?</p>]]></description>
			<author><![CDATA[dummy@example.com (Wickham)]]></author>
			<pubDate>Mon, 02 Nov 2009 20:29:18 +0000</pubDate>
			<guid>http://www.killersites.com/forums/post/11797/#p11797</guid>
		</item>
	</channel>
</rss>
