Jump to content

Inserting datetime using PHP and MySQL


ven.ganeva

Recommended Posts

Hi people, I wonder if someone can help...

 

I have a database field which is of type datetime. I also have a form which inserts a date into the database using a calender. The format of the date is dd/mm/yy. I need help on how I can

 

1) Record the current time

2) Format both the date and time for correct input into the database

 

Any help would be much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/95998-inserting-datetime-using-php-and-mysql/
Share on other sites

<?php
$curdate = date('d/m/y');

echo $curdate . '<br>';

list($day, $month, $year) = explode('/', $curdate);

echo $day . ' - ' . $month . ' - ' . $year . '<br>';
$stamp = mktime(0, 0, 0, $month, $day, $year);

echo date('Y-m-d', $stamp);
?>

Much to much slow php code. Do these directly in your query.

 

To insert the current data/time, use the mysql NOW() function in your query -  http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now

 

Assuming that you have already validated the entered dd/mm/yy date, just use the mysql STR_TO_DATE() function to change it into the correct format in an INSERT or UPDATE query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date

 

I am amazed at how many lines of slow php code gets used to do something that one statement in a query can accomplish.

Assuming that you have already validated the entered dd/mm/yy date, just use the mysql STR_TO_DATE() function to change it into the correct format in an INSERT or UPDATE query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date

 

So what is the correct format for the insert query? I have this but it doesn't work:

 

INSERT INTO tblProcessEvent (TMProcessID, EventTypeID, EventDate) VALUES (2, 1, STR_TO_DATE('26/03/08', '%m/%d/%Y'))

Tried that with no luck. I'm getting the following error:

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '13/03/08', '%d/%m/%y')')' at line 1

 

So it's obviously a problem with the STR_TO_DATE.

I made a quick table with INT, INT, DATE fields and just tried your query in post #6 and it works, so something else about what you are doing is not working. Actual query string -

 

$query = "INSERT INTO tblProcessEvent (TMProcessID, EventTypeID, EventDate) VALUES (2, 1, STR_TO_DATE('20/03/08', '%d/%m/%y'))";

 

Results -

 

TMProcessID EventTypeID EventDate

2 1 2008-03-20

 

 

Still not working I don't understand why  ???

 

This is my full code

 

$insertSQL = "INSERT INTO tblProcessEvent (TMProcessID, EventTypeID, EventDate) VALUES (".$_POST['TMProcessID'].", ". $_POST['EventTypeID'].", STR_TO_DATE('".$_POST['DPC_date1']."', '%d/%m/%y'))";

 

Still getting this error:

 

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '('28/03/08', '%d/%m/%y'))' at line 1

 

help?

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.