This is a discussion on Php -mysql date problem within the PHP Language forums, part of the PHP Programming Forums category; Hello, I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this "newbie&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hello,
I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this "newbie" question... I've found no answer in the forum ... I've a date problem with my formular. In my mysql DB my filed "date" in table "experience" is like this: Y-m-d (2002-07-23). My fied`date` is date, NOT NULL with no default entry My form read well the date data depending the id, (pe. 30.02.2003), but when I submit a new date, I receive as result in the form "30.11.1999" and my DB field I've now "0000-00-00".... I don't know how to correct this... I've lost several hours and I think it's simple... I will appreciate any suggestion and help. Thx for your time, regards, Dominique Here's the php code for my form: <?php require_once('../../../Connections/metadeco_connect.php'); ?> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . date("Y-d-m",strtotime($theValue)) . "'" : "NULL"; break; case "time": $theValue = ($theValue != "") ? "'" . date("H:i:s",strtotime($theValue)) . "'" : "NULL"; break; case "datetime": $theValue = ($theValue != "") ? "'" . date("Y-d-m H:i:s",strtotime($theValue)) . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form_experience_edit")) { $updateSQL = sprintf("UPDATE experience SET `date`=%s WHERE id=%s", GetSQLValueString($_POST['datum'], "date"), GetSQLValueString($_POST['hiddenField'], "int")); mysql_select_db($database_metadeco_connect, $metadeco_connect); $Result1 = mysql_query($updateSQL, $metadeco_connect) or die(mysql_error()); $updateGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?"; $updateGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $updateGoTo)); } mysql_select_db($database_metadeco_connect, $metadeco_connect); $query_rs1experience = "SELECT * FROM experience"; $rs1experience = mysql_query($query_rs1experience, $metadeco_connect) or die(mysql_error()); $row_rs1experience = mysql_fetch_assoc($rs1experience); $totalRows_rs1experience = mysql_num_rows($rs1experience); // *** Move To Specific Record: declare variables $MM_rs = &$rs1experience; $row_MM_rs = &$row_rs1experience; $MM_rsCount = $totalRows_rs1experience; $MM_uniqueCol = "id"; $MM_paramName = "id"; $MM_paramIsDefined = ($MM_paramName != "" && isset($HTTP_GET_VARS[$MM_paramName])); // *** Move To Specific Record: handle detail parameter if ($MM_paramIsDefined && $MM_rsCount != 0) { // get the value of the parameter $param = $HTTP_GET_VARS[$MM_paramName]; // find the record with the unique column value equal to the parameter value do { if ($row_MM_rs[$MM_uniqueCol] == $param) break; } while($row_MM_rs = mysql_fetch_assoc($MM_rs)); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Metadelic :: Portfolio Admin</title> <link href="../../css/metastyle.css" rel="stylesheet" type="text/css"> </head> <body> <div align="center"> <form action="<?php echo $editFormAction; ?>" method="POST" name="form_experience_edit" id="form_experience_edit"> <input name="hiddenField" type="hidden" value="<?php echo $row_rs1experience['id']; ?>"> <table width="95%" border="0" cellspacing="5" cellpadding="3"> <tr> <td align="right" valign="top" bgcolor="#eeeeee">Titre <strong>FR</strong> </td> <td width="100%" align="left" valign="top"> <span class="skills"><img src="../images/arrowlink.gif" width="16" height="16" align="absmiddle"><?php echo $row_rs1experience['titre_fr']; ?></span></td> </tr> <tr> <td align="right" valign="top" bgcolor="#eeeeee">Date</td> <td align="left" valign="top"> <input name="datum" type="text" id="datum" value="<?php echo date('Y.m.d',strtotime($row_rs1experience['date'])); ?>"> <br> <img src="../images/arrowlink.gif" width="16" height="16" align="absmiddle"><span class="skills"><?php echo date('d.m.Y', strtotime($row_rs1experience['date']));?></span></td> </tr> <tr> <td align="right" valign="top" bgcolor="#eeeeee">Confirmation</td> <td align="left" valign="top"><input type="submit" name="Submit" value="Valider"> <input name="Reset" type="reset" value="Reset"></td> </tr> </table> <input type="hidden" name="MM_update" value="form_experience_edit"> </form> </div> </body> </html> <?php mysql_free_result($rs1experience); ?> |
|
|||
|
Dominique Javet wrote:
> I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this > "newbie" question... I've found no answer in the forum ... > I've a date problem with my formular. In my mysql DB my filed "date" in > table "experience" is like this: Y-m-d (2002-07-23). > My fied`date` is date, NOT NULL with no default entry > > My form read well the date data depending the id, (pe. 30.02.2003), but when > I submit a new date, I receive as result in the form "30.11.1999" and my DB > field I've now "0000-00-00".... > > I don't know how to correct this... I've lost several hours and I think it's > simple... > I will appreciate any suggestion and help. MySQL is dumb when it comes to dates! PHP is a little more intelligent but it doesn't compare to human ingenuity. So, you have to transform whatever the user typed, first into something PHP understand, and ultimately to something MySQL understands. I'd go about that by isolating the day, month, and year from the input; then dealing with them appropriately. If your users are always inserting dates as "dd.mm.yyyy" do $input = "30.02.2003"; $input_day = (int)substr($input, 0, 2); // 30 $input_month = (int)substr($input, 3, 2); // 2 $input_year = (int)substr($input, 6, 4); // 2003 if (checkdate($input_month, $input_day, $input_year)) { // date valid } else { // date invalid (the actual $input above will be invalid) } strtotime() accepts a whole lot of formats, but not the one you're using http://www.php.net/strtotime and follow the link there to "Date Input Formats". Happy Coding :) -- USENET would be a better place if everybody read: : mail address : http://www.catb.org/~esr/faqs/smart-questions.html : is valid for : http://www.netmeister.org/news/learn2quote2.html : "text/plain" : http://www.expita.com/nomime.html : to 10K bytes : |
|
|||
|
Pedro Graca wrote:
> Dominique Javet wrote: >> I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for >> this "newbie" question... I've found no answer in the forum ... >> I've a date problem with my formular. In my mysql DB my filed "date" >> in table "experience" is like this: Y-m-d (2002-07-23). >> My fied`date` is date, NOT NULL with no default entry >> >> My form read well the date data depending the id, (pe. 30.02.2003), >> but when I submit a new date, I receive as result in the form >> "30.11.1999" and my DB field I've now "0000-00-00".... >> >> I don't know how to correct this... I've lost several hours and I >> think it's simple... >> I will appreciate any suggestion and help. > > MySQL is dumb when it comes to dates! > PHP is a little more intelligent but it doesn't compare to human > ingenuity. > > So, you have to transform whatever the user typed, first into > something > PHP understand, and ultimately to something MySQL understands. > > I'd go about that by isolating the day, month, and year from the > input; > then dealing with them appropriately. > > If your users are always inserting dates as "dd.mm.yyyy" do > > > $input = "30.02.2003"; > > $input_day = (int)substr($input, 0, 2); // 30 > $input_month = (int)substr($input, 3, 2); // 2 > $input_year = (int)substr($input, 6, 4); // 2003 > if (checkdate($input_month, $input_day, $input_year)) { > // date valid > } else { > // date invalid (the actual $input above will be invalid) > } > > > strtotime() accepts a whole lot of formats, but not the one you're > using http://www.php.net/strtotime > > and follow the link there to "Date Input Formats". > > > Happy Coding :) I would not use substr on that... For example, what if I put in today's date? 1.5.04 - it will take the day as 1. the month as .0 and the year will get nothing. use explode. www.php.net/explode |
|
|||
|
Hello,
Thx a lot for your advise. But, how can I pass the result form the text field into a hidden field? What is the javascript synthax? Because when I first run this script, I receive errors due to the $_POST: <input name="datum" type="text" id="datum" value="<?php echo date('d.m.Y',strtotime($row_rs1experience['date'])); ?>"> <? $date_array = split('[/.-]',$_POST["datum"]);/*This splits out if entered in form as dd/mm/yyyy or dd-mm-yyyy*/ $date_formated = $date_array[2].$date_array[0].$date_array[1];/*This rearranges it into db format*/ echo $date_formated; ?> <input name="hiddenField_datum" type="hidden" value="<?php echo $date_formated; ?>"> Damn.. is so difficult for a newbie like me ;o) A lot of thx for yoru help and time. Regards, Dom |
|
|||
|
I noticed that Message-ID: <c75c1p$i1pu2$1@ID-209842.news.uni-berlin.de>
from Dominique Javet contained the following: ><input name="datum" type="text" id="datum" value="<?php echo >date('d.m.Y',strtotime($row_rs1experience['date'])); ?>"> ><? >$date_array = split('[/.-]',$_POST["datum"]);/*This splits out if entered in >form as dd/mm/yyyy or dd-mm-yyyy*/ >$date_formated = $date_array[2].$date_array[0].$date_array[1];/*This >rearranges it into db format*/ >echo $date_formated; >?> I've done it this way before but I wouldn't bother now. I'd offer the user a series of drop down boxes. No typing involved so the user cannot screw up the input. Then I convert that to a unix timestamp (using strtotime() or mktime() ) and store that as an integer. Easy. -- Geoff Berrow (put thecat out to email) It's only Usenet, no one dies. My opinions, not the committee's, mine. Simple RFDs http://www.ckdog.co.uk/rfdmaker/ |