Solved

Date conversion -- newbie...  php Dreamweaver MX

Posted on 2004-03-21
7
668 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: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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP: concatenate query 13 87
remote mysql 8 37
How to use 2 ON statements in inner join 3 23
Not listening to where 1 18
Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
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://…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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