Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date conversion -- newbie...  php Dreamweaver MX

Posted on 2004-03-21
7
Medium Priority
?
674 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

705 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