0

I need to insert the datetime value entered from the HTML form using PHP into the MySQL database. However I receive the Incorrect datetime value error each time I try to execute the code below,

$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];

//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;

$date = strtotime($rosterstartdate);
echo date('d/M/Y H:i:s', $date);

    // echo DATE_FORMAT($rosterstartdate,"%Y%m%d %H%i%s");

    $con=mysql_connect("localhost","root","");  
    if($con==true){
        mysql_select_db("attendance_db",$con);
        $query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts','$date','$rosterenddate')";

I have tried using each of the different techniques above to do the conversion but it does not work. Any ideas on how this could be inserted

4
  • What is the exact error you are getting? Would you post the output of 'describe tblroster;' ? Commented Jul 22, 2012 at 8:32
  • The date format should be YYYY-MM-DD HH:MM:SS Commented Jul 22, 2012 at 8:33
  • Incorrect datetime value: '1341926751' for column 'rosterstartdate' at row 1 Commented Jul 22, 2012 at 8:33
  • What is the column type of 'rosterstartdate'? Commented Jul 22, 2012 at 9:19

2 Answers 2

2

try this:

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

Instead of echoing it out, use that code to format the date.

However, it looks like what you really want is this:

$rosterstartdate = date('Y-m-d H:i:s', strtotime($rosterstartdate));
$rosterenddate = date('Y-m-d H:i:s', strtotime($rosterenddate));

This way you can just reference those two variables in your query string.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to format it if you have a unixtime use FROM_UNIXTIME,
change your query as

    $rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];

//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;

$date = strtotime($rosterstartdate);

    $con=mysql_connect("localhost","root","");  
    if($con==true){
        mysql_select_db("attendance_db",$con);
        $query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts',FROM_UNIXTIME($date),'$rosterenddate')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.