Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP: Inserting date into a table

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
Hi everyone,
My table "list" contains one field: "exam_date" of type "datetime".
Dates in that table are formatted by the following structure: "2014-08-31 21:45:47" (yyyy-mm-dd hh:ii:ss).
In my input form date is assigned the following way:
Code:
 <input id="exam_date" name="exam_date" type="datetime" placeholder="<?php echo date('d-m-y H:i:s'); ?>">
In order to see the value assigned I added to my code:
Code:
$myDate = trim($_POST['exam_date']);
echo trim($_POST['exam_date']);
On the display i see the date inserted by the following structure:31-08-14 21:45:47.
My code to insert the new value to the table is:
Code:
<?php
date_default_timezone_set('xxx');
$dbcon = @mysqli_connect ('xxx', 'xxx', 'xxx', 'test') OR die ('Could not connect to MySQL: ' . mysqli_connect_error () ); 
mysqli_set_charset($dbcon, 'utf8');
if (isset($_POST['exam_date']))
{
$myDate = trim($_POST['exam_date']);
echo $myDate ;
$q = "INSERT INTO list (exam_date) VALUES ($myDate)"; 
$result = @mysqli_query ($dbcon, $q); 
if($result)
{
echo "record added";
}
else
{
echo "no record added !";
}
}
?>
<form action="add_date.php" method="post">
<input id="exam_date" name="exam_date" type="datetime" placeholder="<?php echo date('y-m-d H:i:s'); ?>">
<input id="submit" type="submit" name="submit" value="Register">
</form>
When I run it i get: "no record added" and when I check the table I see that no record was added indeed.
I belive that the reason for not able to add a record is the difference in date structures' that of the table and that of the input.
Can I change the date structure in the table to "dd-mm-yy hh:ii:ss"?
Is there a way to convert the input structure from "dd-mm-yy hh-ii-ss" to "yyyy-mm-dd hh:ii:ss"?
Thanks !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top