Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Insert US date to MySQL format

New Here ,
Feb 13, 2008 Feb 13, 2008
Having problems inserting MM-DD-YYYY to Y-m-d (MySql default format). After many hours of frustration, I purchased the Developer's toolbox thinking it would handle this automatically. yes, very funny. The Developer toolbox control panel requires the date format setting for database format and screen format. I used the Date picker form control to insert a date, but it errors on insert (putting 0000-00-00 instead of the actual date inserted).

Does anyone have suggestions on how to convert the date to Y-m-d?

Here's the insert code (using an extension that allows inserting of multiple rows using repeat region) Trying to get the delduedate formatted as Y-m-d. Thanks in advance.


// *** FX Insert Multiple Records RL in add_deliverable
if (isset($startRow_deliverabletype)) $ins_deliverabletype = $startRow_deliverabletype; else $ins_deliverabletype = 0; // counter
if (isset($_POST['Submit'])) {
$FX_sqlerror = "";
$FX_insredir = "view_conf_event.php";
$FX_fields = array("deltypeid","eventid","delduedate","delstatusid","delassignee","delnotes");
$FX_altv = false;
mysql_select_db($database_eventtracker, $eventtracker);
for ($N=$ins_deliverabletype+1; $N<=$totalRows_deliverabletype; $N++) {
$found = false;
for ($i=0; $i<sizeof($FX_fields); $i++) {
if ((isset($_POST[$FX_fields[$i].$N]) && $_POST[$FX_fields[$i].$N] != "") || $FX_altv) {
$found = true; break;
}
}
if ($found) {
$mIns_SQL = sprintf("INSERT INTO deliverables (deltypeid, eventid, delduedate, delstatusid, delassignee, delnotes) VALUES (%s, %s, %s, %s, %s, %s)",
isset($_POST["deltypeid$N"]) ? ($_POST["deltypeid$N"] != "" ? ("".addslashes(stripslashes($_POST["deltypeid$N"]))."") : "NULL") : "0",
isset($_POST["eventid$N"]) ? ($_POST["eventid$N"] != "" ? ("".addslashes(stripslashes($_POST["eventid$N"]))."") : "NULL") : "0",
isset($_POST["delduedate$N"]) ? ($_POST["delduedate$N"] != "" ? ("'".addslashes(stripslashes($_POST["delduedate$N"]))."'") : "NULL") : "''",
isset($_POST["delstatusid$N"]) ? ($_POST["delstatusid$N"] != "" ? ("".addslashes(stripslashes($_POST["delstatusid$N"]))."") : "NULL") : "0",
isset($_POST["delassignee$N"]) ? ($_POST["delassignee$N"] != "" ? ("'".addslashes(stripslashes($_POST["delassignee$N"]))."'") : "NULL") : "''",
isset($_POST["delnotes$N"]) ? ($_POST["delnotes$N"] != "" ? ("'".addslashes(stripslashes($_POST["delnotes$N"]))."'") : "NULL") : "''",
"");
$mIns_Result = mysql_query($mIns_SQL, $eventtracker) or $FX_sqlerror .= "Row ".$N.": " . mysql_error() . "<br><br>";
}
}
if ($FX_insredir == "") $FX_insredir = basename($_SERVER['SCRIPT_NAME']);
if (isset($_SERVER['QUERY_STRING'])) {
$FX_insredir .= (strpos($FX_insredir, '?')) ? "&" : "?";
$FX_insredir .= $_SERVER['QUERY_STRING'];
}
if ($FX_sqlerror != "") {
echo '<font color="red">'.$FX_sqlerror.'</font>';
} else header("Location: " . $FX_insredir);
}
TOPICS
Server side applications
341
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 14, 2008 Feb 14, 2008
LATEST
driscoll5 wrote:
> Having problems inserting MM-DD-YYYY to Y-m-d (MySql default format).

function UStoMySQL($date) {
$parts = preg_split('{[-/\s]}', $date);
if (is_array($parts) && count($parts == 3) && checkdate($parts[0],
$parts[1], $parts[2])) {
return "$parts[2]-$parts[0]-$parts[1]";
} else {
return 'Invalid date';
}
}

$date = '2/14/2008';
$convertedDate = UStoMySQL($date);

// $convertedDate is 2008-2-14 (leading zeros are optional in MySQL)

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines