Jump to content

date formatting


fife

Recommended Posts

hi.  I have a simple form to get a users details and one of the details is D.O.B formatted dd/mm/yy.  Now when it goes to my sql database its my understanding it has to be yy/mm/dd.  Im new to this and ive search high and low for the answer.  can anyone help?

Here is a short version of the php code and a very short version of the html:

 

<?php
$insertform = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {
  $insertform .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["insert"])) && ($_POST["insert"] == "insertform")) {
  
$insertSQL = sprintf("INSERT INTO Members (`FirstName`, `LastName`, `DOB`, ) VALUES %s, %s, %s)",
                       GetSQLValueString($_POST['FirstName'], "text"),
                       GetSQLValueString($_POST['LastName'], "text"),
                       GetSQLValueString($_POST['DOB'], "date"),

mysql_select_db($database_db, $db);
  $Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
?>

<html>
<body>
<form action="<?php echo $insertform; ?>" method="post" name="insertform" id="insertform">

<input type="text" name="FirstName" value="" size="20" />
<input type="text" name="LastName" value="" size="20" />
<input type="text" name="DOB" size="15" />
</form>
</body>
</html>

Link to comment
Share on other sites

MySQL stores dates as  YYYY-MM-DD

 

there are a zillion things you can do.  look into strtotime() and date() functions.

 

The simplest thing though, since you know the format coming in and just need to convert it is to explode the string and reorganize the values:

 

<?php

$users_input_dob = "31/12/95";
$parts = explode("/",$users_input_dob);  //$parts[0] = 31  $parts[1] = 12 $parts[2] = 95
$newDate = "19".$parts[2]."-".$parts[1]."-".$parts[0];

echo $newDate;  //returns 1995-12-31

 

Link to comment
Share on other sites

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.