Jump to content

Recommended Posts

I tried as below it didn't work

 

$birthdate = substr($birthdate,6,4) . "-" . substr($birthdate,0,2) . "-" . substr($birthdate,3,2);

 

@jpacs what are you trying to accomplish with the function substr and the numbers inside the parenthesis after the variable? please explain. It didn't work for me I used the code and enter the birthdate in the following format 10-19-2000 and in the database there was a value of 0000-00-00 in the birthdate database field.

 

What do you mean by hyphons @dawsha?

 

 

 

lol @ my spelling

The hyphen ( ‐ ) is a punctuation mark used to join words and to separate syllables of a single word. It should not be confused with dashes ( –, —, ― ), which are longer and have different uses, and with the minus sign ( − ) which is also longer. The use of hyphens is called hyphenation. In environments that are restricted to ISO 646 the hyphen-minus is used as a replacement.

http://en.wikipedia.org/wiki/Hyphen

if your outputting the date into the sql with hyphens, then the rule applied has to contain the map

 

so if your DOB is : 16/05/1979 then the SQL is STR_TO_DATE('16/05/1979','%d/%m/%Y')

so if your DOB is : 16-05-1979 then the SQL is STR_TO_DATE('16-05-1979','%d-%m-%Y')

so if your DOB is : 16?05?1979 then the SQL is STR_TO_DATE('16?05?1979','%d?%m?%Y')

so if your DOB is : 16_05_1979 then the SQL is STR_TO_DATE('16_05_1979','%d/%m/%Y')

so if your DOB is : 05/16/1979 then the SQL is STR_TO_DATE('05/16/1979','%m/%d/%Y')

so if your DOB is : 05-16-1979 then the SQL is STR_TO_DATE('05-16-1979','%m-%d-%Y')

so if your DOB is : 05?16?1979 then the SQL is STR_TO_DATE('05?16?1979','%m?%d?%Y')

 

and so on and so on :) have fun

A DATE data type field expects the date to be formatted as YYYY-MM-DD. How are you getting the date from the form?

 

Post the form and the part of the code that processes the date field(s). You aren't using an <input type="text"> field for this are you?

In the form I am entering YYYY-MM-DD

 

I am using the type for the input as "text", There might be a problem because the form is carrying the data as a text and the user is entering numbers.

 

<label for="Birthdate">Birthdate: </label><input type="text" name="birthdate" /><font size="-2" color="#006600">(Year-Month-Day)</font>

 

 

So that might be the problem. what would be the most appropriate type attribute for this purpose?

 

Form.php

<form action="InsertForm.php" method="post">

<label for="Birthdate">Birthdate: </label>
<input type="text" name="birthdate" />
<font size="-2" color="#006600">(Year-Month-Day)</font>

</form>

 

 

INsertForm.php

if (isset($_POST['submitted'] )){
include "storescripts/connect_to_mysql.php";
$birthdate = $_POST['birthdate'];
$sqlinsert = "INSERT INTO costumers (birthdate) VALUES ($birthdate)";

$enterquery = mysql_query($sqlinsert) or die(mysql_error()); 

}

 

The code above insert but User need to type Year first YYYY-MM-DD and I would like it to be MM-DD-YYYY.

 

 

Even if that isn't the entire problem right now, it will be as soon as the site is live. There's no real way to enforce that the user enter the date in the right format in a text field. Use a Javascript date picker such as the one in the JQuery framework with a <noscript> section using 3 <select> fields, one for month, day and year.

no so true pikachu, what if the user has JS disabled or a JS bug exists further in the head, although i use jquery calenders all the time, if you want pure and simple use dropdowns

<select name="day">
  <?
  for($i=1;$i<=31;$i++)
  {
    ?>
      <option value="<?= $i; ?>"><?= $i; ?></option>
    <?
  }
  ?>
</select>
<select name="month">
  <?
  for($i=1;$i<=12;$i++)
  {
    ?>
      <option value="<?= $i; ?>"><?= $i; ?></option>
    <?
  }
  ?>
</select>
<select name="Year">
  <?
  for($i=2010;$i>=1930;$i--)
  {
    ?>
      <option value="<?= $i; ?>"><?= $i; ?></option>
    <?
  }
  ?>
</select>

mash it up in php before injecting into sql

$birthdate = trim($_POST['Year'].'-'.$_POST['month'].'-'.$_POST['day']);

just for kicks :)

. . . Use a Javascript date picker such as the one in the JQuery framework with a <noscript> section using 3 <select> fields, one for month, day and year.

 

As I said . . .

Why you suggest Javascript on top of php?

 

Good one dawsa!

 

I have two other solutions I would like to discuss and see your opinion.

 

First one a very painfull one for the programer.

 

Make three tables one for "year" "month" and day"

 

create id,-- year, month or day field and a foreign key in each table to later link.

 

Then make three queries jam it into a variable, then echo the variable result set into a s<elect><option> html fields to populate the results from those queries.

 

Let the user choose what he/she wants choose in the year, month and day html form input fields and submit the form into table costumer fields year, month and day.

 

Second

 

Easier to create three input fields assign a name to the input field of  year, month and the last one day. Then submit the values into the costumer table fields year, month and day.

 

 

Which one of these three approaches --the one from dawsa with the for loop and the two above this question? Which one would be more efficient?  I don't include the javascript option from pikachu because I don't know that much javascript language. 

Neither of those approaches is any good. The date needs to be entered into ONE date field in ONE table in the database in YYYY-MM-DD format if you plan to actually be able to use it for anything with any degree of ease.

 

You don't need to know much about javascript to use a JQuery datepicker; I know next to nothing about javascript, and have no problem reading the documentation for a datepicker and making it work. But since you don't want to use one here's what I'd recommend doing. Try it in a new script to see how it works, then insert it into your script.

 

EXAMPLE:

<?php
if( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
   $date = "{$_POST['year']}-{$_POST['month']}-{$_POST['day']}";
   echo "Formatted date is: $date"; // THIS VALUE CAN BE INSERTED DIRECTLY INTO THE DATABASE
}
echo "<form action=\"\" method=\"post\">";

$days = range(1, 31);
$years = range( date('Y'), (date('Y') - 100) );
$months = array( 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

echo "<select name=\"month\">\n";
foreach( $months as $k => $v ) {
   $sel = isset($_POST['month']) && $_POST['month'] == $k ? 'selected="selected"' : '';
   echo "<option value=\"" . sprintf('%02d', $k) . "\" $sel>$v</option>\n";
}
echo "</select>\n";

echo"<select name=\"day\">\n";
foreach( $days as $v ) {
   $sel = isset($_POST['day']) && $_POST['day'] == $v ? 'selected="selected"' : '';
   echo "<option value=\"" . sprintf( '%02d', $v) . "\" $sel>$v</option>\n";
}
echo "</select>\n";

echo "<select name=\"year\">\n";
foreach( $years as $v ) {
   $sel = isset($_POST['year']) && $_POST['year'] == $v ? 'selected="selected"' : '';
   echo "<option value=\"$v\" $sel>$v</option>\n";
}
echo "</select>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\">\n</form>";
?>

Neither of those approaches is any good. The date needs to be entered into ONE date field in ONE table in the database in YYYY-MM-DD format if you plan to actually be able to use it for anything with any degree of ease.

 

You don't need to know much about javascript to use a JQuery datepicker; I know next to nothing about javascript, and have no problem reading the documentation for a datepicker and making it work. But since you don't want to use one here's what I'd recommend doing. Try it in a new script to see how it works, then insert it into your script.

 

EXAMPLE:

<?php
if( strtolower($_SERVER['REQUEST_METHOD']) == 'post' ) {
   $date = "{$_POST['year']}-{$_POST['month']}-{$_POST['day']}";
   echo "Formatted date is: $date"; // THIS VALUE CAN BE INSERTED DIRECTLY INTO THE DATABASE
}
echo "<form action=\"\" method=\"post\">";

$days = range(1, 31);
$years = range( date('Y'), (date('Y') - 100) );
$months = array( 1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');

echo "<select name=\"month\">\n";
foreach( $months as $k => $v ) {
   $sel = isset($_POST['month']) && $_POST['month'] == $k ? 'selected="selected"' : '';
   echo "<option value=\"" . sprintf('%02d', $k) . "\" $sel>$v</option>\n";
}
echo "</select>\n";

echo"<select name=\"day\">\n";
foreach( $days as $v ) {
   $sel = isset($_POST['day']) && $_POST['day'] == $v ? 'selected="selected"' : '';
   echo "<option value=\"" . sprintf( '%02d', $v) . "\" $sel>$v</option>\n";
}
echo "</select>\n";

echo "<select name=\"year\">\n";
foreach( $years as $v ) {
   $sel = isset($_POST['year']) && $_POST['year'] == $v ? 'selected="selected"' : '';
   echo "<option value=\"$v\" $sel>$v</option>\n";
}
echo "</select>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\">\n</form>";
?>

 

Your code work together with grouping an array of the POST fields year, month and day inside a variable just like @dawsa has it.  Then Insert the fields and the values through an INSERT statement. It did worked. I did two INSERT since I was going to insert data into two different tables through the same form.

 

I am surprise to know and realize in this script that I don't necessarily need to pull the month, day or year data from the tables in the database but generate those with math calculations as Pikachu did in the variables $day, $month and $year then distribute those calculations in the form.

 

Great approach and calculos. I have to admit some of the functions I need to review in order to understand your script.

 

Thank you.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.