Solved

Date conversion -- newbie...  php Dreamweaver MX

Posted on 2004-03-21
7
671 Views
Last Modified: 2008-03-06
Background:

DW MX 2004  -  vdaemon field validation extension installed
IIS 6
mySQL & PHP current versions

Everything posts and works fine.  I'm trying to tweak a date of birth field that will allow users to enter a mm-dd-yyyy and then have it update the mySQL db with the YYYY-mm-dd format  mySQL requires.

I've found a script to make the conversion.  I'm having troouble understanding how the form field (t_date) gets into the conversion (input2date() function and then loaded into the insert SQL command.  

Here's form input string:

  <input name="t_date" type="text" id="t_date">

Here's datacheck.php I'm calling to convert:

function input2date($form_date)
{
  $token="-./ ";

  $p1 = strtok($form_date,$token);
  $p2 = strtok($token);
  $p3 = strtok($token);
  $p4 = strtok($token);

  $flipped_date="";
  $y=""; $m=""; $d="";

  // check 'd.m.y'
  if (($p1>0 && $p1<32) && 
      ($p2>0 && $p2<13) && 
      ($p3>32))
  {
    $y=$p3;
    $m=$p2;
    $d=$p1;
  }

  // check 'y.m.d'
  if ($y == "" &&
      ($p1>32) && 
      ($p2>0 && $p2<13) && 
      ($p3>0 && $p3<32))
  {
    $y=$p1;
    $m=$p2;
    $d=$p3;
  }

  // check 'd.m'
  if ($y == "" &&
      ($p3=="") && 
      ($p2>0 && $p2<13) && 
      ($p1>0 && $p1<32))
  {
    $y=date("Y");
    $m=$p2;
    $d=$p1;
  }

  // check 'd'
  if ($y == "" &&
      ($p3=="") && 
      ($p2=="") && 
      ($p1>0 && $p1<32))
  {
    $y=date("Y");
    $m=date("m");
    $d=$p1;
  }

  // add 1900 or 2000 to year
  if ($y!="" && $y<=99)
  {
    if ($y>=70) $y = $y + 1900;
    if ($y<70) $y = $y + 2000;
  }

  if ($y!="")
  {
    if (checkdate($m, $d, $y))
      $date="$y-$m-$d";
  }

  return $flipped_date;
}
?>

Here's the SQL load:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO date_test (T_Comment, T_date) VALUES (%s, %s)",
                       GetSQLValueString($_POST['t_com'], "text"),
                       GetSQLValueString($_POST['t_date'], "date"));

THANKS in advance for your time in reviewing and assisting me.
0
Comment
Question by:jdekeyrel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:Andy
ID: 10646570
That seems like an awful lot of code to convert this date. There are PHP functions for converting dates but as you're dealing with date of birth chances are you may get someone born before 1970 and they won't help you then because they can't go father back than that. So it's prolly easiest to split up your string and rearrange it because you know that each section is separated by a -.

$dateparts = split("-", $_POST['t_date']);
$newdate = "$dateparts[2]-$dateparts[1]-$dateparts[0]";

So putting 23-03-1975 through that just swaps round the year and the day to give you 1975-03-23.
0
 

Author Comment

by:jdekeyrel
ID: 10650235
I felt the same way but wasn't sure...

What if they use /? instead of -  ?

Where do I put $newdate in my SQLinsert string?

THANKS

0
 
LVL 6

Expert Comment

by:Andy
ID: 10650456
I take it you just have one textbox field for date of birth?

You could either replace any occurence of / with - like this...

$newdate = preg_replace("/","-", $_POST['name_of_your_date_box']);

and then do...

$dateparts = split("-", $newdate);
$dob = "$dateparts[2]-$dateparts[1]-$dateparts[0]";

Or you could make it into three separte fields for Day, Month and Year and then join them together with whatever separator you want in whatever order you want...

$day = $_POST['name_of_your_day_box'];
$month = $_POST['name_of_your_month_box'];
$year = $_POST['name_of_your_year_box'];

$dob = "$year-$month-$day";

Insert statement would be...

"INSERT INTO name_of_table (name_of_date_field, some_other_field, etc...) VALUES ('$dob', 'some_value', etc...)"

Hope that helps.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:jdekeyrel
ID: 10651361
Almost --

Here's where I added it.....

 $newdate = preg_replace("/","-", $_POST['t_date']);
 $dateparts = split("-", $newdate);
 $dob = "$dateparts[2]-$dateparts[1]-$dateparts[0]";

I've tried a number of varitions on your insert statement.  Sometimes the comments post but NEVER the date.   '$dob' added in place of the %s doesn't work and adding $dob after the GetSQL statement doesn't either.

  $insertSQL = sprintf("INSERT INTO date_test (T_Comment, T_date) VALUES (%s, %s)",
                       GetSQLValueString($_POST['com'], "text");

THANKS AGAIN THOUGH ---
0
 

Author Comment

by:jdekeyrel
ID: 10651893
Here's the who section:

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 != "") ? "'" . $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 ($oVDaemonStatus && $oVDaemonStatus->bValid && isset($HTTP_POST_VARS["MM_insert"]) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {

$newdate = preg_replace("/","-", $_POST['t_date']);
 $dateparts = split("-", $newdate);
 $dob = "$dateparts[2]-$dateparts[1]-$dateparts[0]";

I've tried a number of varitions on your insert statement.  Sometimes the comments post but NEVER the date.   '$dob' added in place of the %s doesn't work and adding $dob after the GetSQL statement doesn't either.

  $insertSQL = sprintf("INSERT INTO date_test (T_Comment, T_date) VALUES (%s, %s)",
                       GetSQLValueString($_POST['com'], "text");
0
 
LVL 6

Accepted Solution

by:
Andy earned 500 total points
ID: 10652485
OK... The functions built into Dreamweaver are fine for pulling out data but when it comes to putting it in the database it's easier to do it yourself. Also, validate the form yourself using PHP not the extension then you can give better feedback to the user on the errors.

So you have your form on one page and the action of the form posts the values to the next page. At the top of that page you can do this...

<?php
require_once('YourConn.php');

$day = $_POST['name_of_your_day_box'];
$month = $_POST['name_of_your_month_box'];
$year = $_POST['name_of_your_year_box'];
//Get the rest of your form fields here as well

//This next bit is just an example of validating it yourself
$errors = array();
if($day == ""){
     array_push($errors, "You did note enter the day of your birth!");
}
if($month == ""){
     array_push($errors, "You did note enter the month of your birth!");
}
if($year == ""){
     array_push($errors, "You did note enter the year of your birth!");
}
//Now all those error messages are stored in the array $errors for later use.

//If $errors is empty then we can add to the DB
if(count($errors) == 0){
     $dob = "$year-$month-$day";

     mysql_select_db($database_YourConn, $YourConn);
     $insert= "INSERT INTO date_test SET t_date = '$dob'";
     $result = mysql_query($insert) or die(mysql_error());
}
?>

Now do your HTML. Inside the body tags you can do...

<?php
if(count($errors) > 0){ ?>
     The following errors occured...
     <?php
     foreach($errors as $e){
          echo "$e<br>";
     }
     ?>
     Please click the back button to correct these mistakes!
<?php
}
else { ?>
     HTML in here appropriate for correctly filled out form.
<?php
}
?>
0
 

Author Comment

by:jdekeyrel
ID: 10652916
THANKS -- You've been great.    Not sure I'm up to the task of rewriting the page....  
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question