Forums

PHP

This topic is locked

Covnvert date in a form field

Posted 13 Nov 2006 03:05:08
1
has voted
13 Nov 2006 03:05:08 Robert Robinette posted:
Now Let me put this in the right forum category ...


I'm trying to pass two dates from a form and use them as variables in a SQL select statement. Everything works properly, but to make it work I have to enter the dates in the form field in MySQL date format - yyyy-mm-dd.

Is there a way to allow the user to enter the date in another format mm/dd/yyyy, pass the date as a variable in MySQL date format, and still make the SQL query work?

Thanks to anyone who can point me in the right direction.

Replies

Replied 13 Nov 2006 04:08:07
13 Nov 2006 04:08:07 LorD ExoskeletoN replied:
hi maybe you can enter your own format like mm/dd/yyyy but in the coding you can make use of explode or split function to convert it to yyyy-mm-dd

see the links:

au2.php.net/manual/en/function.explode.php
au2.php.net/manual/en/function.split.php

hope it'l helps..good luck more power

<div align=right>
afraid NOT to FAIL for you learn NOTHING

www.nasbikesphilippines.bravehost.com
<img src="www.motorcyclephilippines.com/forums/signaturepics/sigpic14460_2.gif" border=0></div id=right>
Replied 13 Nov 2006 07:17:16
13 Nov 2006 07:17:16 Robert Robinette replied:
Thanks for the advice. However I'm not that proficient in PHP. This looks far too complicated for me to implement. <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>
Replied 13 Nov 2006 09:28:07
13 Nov 2006 09:28:07 LorD ExoskeletoN replied:
<pre id=code><font face=courier size=2 id=code>
&lt;?php
// Delimiters may be slash, dot, or hyphen

// assuming this is your input pass to variable $date
$date = "04/30/1973";

// this split function separates the values each time it reads slash(/),comma(,) or hyphen(-)
// but you can set that one

list($month, $day, $year) = split('[/.-]', $date);

// you can save it now to database in this format yyyy-mm-dd, look at the values below
$SQL = "INSERT INTO tbl_name (column_date) VALUES ($year-$month-$day)";

// execute the sql_query
$exe = @mysql_query($SQL) or die(@mysql_error());

// so the date format now was yyyy-mm-dd that has been saved to database
//coz we intechanged the values

?&gt;
</font id=code></pre id=code>

robert hope this will help...

<div align=right>
afraid NOT to FAIL for you learn NOTHING

www.nasbikesphilippines.bravehost.com
<img src="www.motorcyclephilippines.com/forums/signaturepics/sigpic14460_2.gif" border=0></div id=right>

Edited by - exoskeleton on 13 Nov 2006 10:09:15

Edited by - exoskeleton on 13 Nov 2006 10:09:55

Edited by - exoskeleton on 13 Nov 2006 10:10:19
Replied 13 Nov 2006 17:02:35
13 Nov 2006 17:02:35 Robert Robinette replied:
Works perfectly. Exactly what I was looking for. Thanks for the explanation - and thanks to everyone who takes the time to answer in these forums. Very much appreciated.
Replied 14 Nov 2006 10:01:49
14 Nov 2006 10:01:49 LorD ExoskeletoN replied:
my pleasure...

<div align=right>
afraid NOT to FAIL for you learn NOTHING

www.nasbikesphilippines.bravehost.com
<img src="www.motorcyclephilippines.com/forums/signaturepics/sigpic14460_2.gif" border=0></div id=right>
Replied 17 Nov 2006 01:16:57
17 Nov 2006 01:16:57 Robert Robinette replied:
I amended my code slightly as I want to incorporate a javascript Date Picker. In doing so I've caused the page to work properly.

I'm trying to convert the mm-dd-yyyy format to MySql format (yyyy-mm-dd) when the form variable is passed. I'm trying to use the split function to do this. Can someone look at my code and tell if they see where the problem is? Recordset produced is always based on the default values ( $startDate_rsAV = "01/03/2007" & $endDate_rsAV = "01/04/2007" and not the input values. I'm getting pretty frustrated as I've been at this all day.

Thanks to all.

<pre id=code><font face=courier size=2 id=code>
&lt;?php
$startDate_rsAV = "01/03/2007";
if (isset($_POST['startDate'])) {
$startDate_rsAV1 = (get_magic_quotes_gpc()) ? $_POST['startDate'] : addslashes($_POST['startDate']);
}
list($sMonth, $sDay, $sYear) = split('[/.-]', $startDate_rsAV);

$endDate_rsAV = "01/04/2007";
if (isset($_POST['endDate'])) {
$endDate_rsAV1 = (get_magic_quotes_gpc()) ? $_POST['endDate'] : addslashes($_POST['endDate']);
}
list($eMonth, $eDay, $eYear) = split('[/.-]', $endDate_rsAV);

mysql_select_db($database_connGR, $connGR);
$query_rsAV = "SELECT * FROM calendar WHERE calDate &gt;= ' ($sYear-$sMonth-$sDay)' AND calDate &lt; '($eYear-$eMonth-$eDay'";
$rsAV = mysql_query($query_rsAV, $connGR) or die(mysql_error());
$row_rsAV = mysql_fetch_assoc($rsAV);
$totalRows_rsAV = mysql_num_rows($rsAV);
?&gt;


</font id=code></pre id=code>
Replied 20 Nov 2006 18:44:37
20 Nov 2006 18:44:37 Roddy Dairion replied:
If you're incorporating a javascript date picked then the split function doesn't need to be used as you can already specify in the javascript date picker which format you want the date to appear in the textbox.

Reply to this topic