PHP MySQL syntax for inserting date,time

mysqlphpsyntax

I am trying to insert to date,time fields using a php script but I am getting a syntax error. Can someone please tell me, where I am doing the mistake.
Thanks fellows

INSERT INTO calendar(event,from,to,day)
VALUES
('".$_REQUEST['event']."',
'".$_REQUEST['from_time']."',
'".$_REQUEST['to_time']."',
'".$_REQUEST['date_event']."')

Best Solution

To insert into mySQL datetime fields, the string has to be in a certain format for mySQL to understand it. The problem is that php has its own thoughts and ideas on how dates are represented. When dealing with dates between the two you have to translate.

If in php you are dealing with a time object you can do this to get a string mySQL will like:

$mysqlDateString = date('Y-m-d H:i:s', $phpdate);

or if you are dealing with a string date you can do this:

$mysqlDateString = date('Y-m-d H:i:s', $strtotime("08/09/2009"));

If you get a datetime string from mySQL you can do this to deal with it in PHP:

$phpTime = strtotime($mysqlDateString);

Just came across this problem myself, so hopefully this will work for you as well.