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
https://forums.phpfreaks.com/topic/151249-date-formatting/
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
https://forums.phpfreaks.com/topic/151249-date-formatting/#findComment-794506
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.