Solved

Date conversion -- newbie...  php Dreamweaver MX

Posted on 2004-03-21
7
660 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
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:Reapz
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:Reapz
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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:
Reapz 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now