Solved

Date conversion -- newbie...  php Dreamweaver MX

Posted on 2004-03-21
7
666 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
myqsl update statement on phpMyAdmin 8 30
How to send multiple emails at the same time in PHP 12 57
MySqlDump not dumping triggers 1 30
mysql update statement 3 22
A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

786 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