ankit.pandeyc012 Posted September 24, 2011 Share Posted September 24, 2011 <?php $myServer = "202.138.125.155"; $myUser = "micro_fms"; $myPass = "micro_fms*#$"; $myDB = "micro_fms"; //create an instance of the ADO connection object $conn = new COM("ADODB.Connection")or die("Cannot start ADO"); //define connection string, specify database driver $connStr = "PROVIDER='SQLOLEDB'; SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; //Open the connection to the database $conn->open($connStr); //$table_name=$_GET['Device']; $table_data=$_GET['Data']; $data=(explode(",",$table_data)); $table_name=$data[0]; $speed=$data[1]; $time=$data[2]; $date=$data[3]; $lat=$data[4]; $long=$data[5]; //$newdate = substr($date,6,2) . "-" . substr($date,0,2) . "-" substr($date,3,2); $datetime=$date.$time; $new_lat = substr($lat,0,2).'.'.substr($lat,2); $new_long = substr($long,0,2).'.'.substr($long,2); echo $table_name."<br>"; echo $speed."<br>"; echo $new_lat."<br>"; echo $new_long."<br>"; echo $datetime."<br>"; $query="insert into $table_name (DeviceNumber,Speed,dtDateTime,Latitude,Longitude)values('$table_name','$speed','$datetime','$new_lat','$new_long')"; //execute the SQL statement and return records $rs = $conn->execute($query); if($rs) { echo 'Values Inserted'; } else { echo "faile"; } ?> Hi friends... I have two variables ($date and $time). I concat these two strings and make a new variable called "$datetime". Now in above code I want to insert it into table but I got the following error. Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft OLE DB Provider for SQL Server<br/><b>Description:</b> Conversion failed when converting datetime from character string.' in C:\wamp\www\FMS\fms.php:44 Stack trace: #0 C:\wamp\www\FMS\fms.php(44): com->execute('insert into A00...') #1 {main} thrown in C:\wamp\www\FMS\fms.php on line 44 In my database, data type of column in which I want to insert is Date time stamp though I have string type ($datetime) variable. I think I have to convert this $datetime variable string to "DateTime type first but don't have any idea about it. Please help??/ Thanks in advance.... Quote Link to comment https://forums.phpfreaks.com/topic/247763-how-to-convert-string-into-datetime-stamp-in-php/ Share on other sites More sharing options...
Adam Posted September 24, 2011 Share Posted September 24, 2011 The database should automatically convert it, provided it's in a readable format. What's likely the problem is you don't have a space between the date and time. Should be: $datetime=$date . ' ' . $time; If that doesn't fix it, please post a var_dump() of the $datetime variable. Quote Link to comment https://forums.phpfreaks.com/topic/247763-how-to-convert-string-into-datetime-stamp-in-php/#findComment-1272297 Share on other sites More sharing options...
ankit.pandeyc012 Posted September 24, 2011 Author Share Posted September 24, 2011 Thanks for your reply.. It works but my purpose is still not solved. It not inserts in database and throw out of range error. and I have a one more requirement also. Now, I want in $time variable add 5:30:00 and store in time format( so that it can be insert it in database) and I have date in ddmmyy format but in string type which i have to convert it in date mm-dd-yyformat ( again so that it can be insert it in database). How can I do these??? Quote Link to comment https://forums.phpfreaks.com/topic/247763-how-to-convert-string-into-datetime-stamp-in-php/#findComment-1272311 Share on other sites More sharing options...
ankit.pandeyc012 Posted September 24, 2011 Author Share Posted September 24, 2011 Hi friends... I have a variable $datetime= 140811 060632. Here 140811 represents date in ddmmyy format and 060632 represents time 06:06:32 and both are in string type. I have a column name "DtTime" in MSSQL of type "Datetime" stamp. Now when i try to insert my variable $datetime in above column of database it returns an "out of memory" error which means I think I have to convert my string to "datetime" stamp type first and then try to insert. I don't know how I can convert string to datetime type so that it inserts in column of "datetime" stamp type. Please help... Thanks in advance..... Quote Link to comment https://forums.phpfreaks.com/topic/247763-how-to-convert-string-into-datetime-stamp-in-php/#findComment-1272319 Share on other sites More sharing options...
xyph Posted September 24, 2011 Share Posted September 24, 2011 <?php $db = new MySQLi( 'localhost','root','','db' ); $date = '140811 060632'; $d = substr( $date,0,2 ); $mo = substr( $date,2,2 ); $y = substr( $date,4,2 ); $h = substr( $date,7,2 ); $m = substr( $date,9,2 ); $s = substr( $date,11,2 ); $sql_date = $y.$mo.$d.$h.$m.$s; echo $sql_date.'<br>'; $q = 'INSERT INTO `users` (`name`, `last_activity`) VALUES ("test_user", "'.$sql_date.'")'; if( !$db->query($q) ) trigger_error('INSERT error',E_USER_ERROR); $q = 'SELECT `id`, `last_activity` FROM `users` WHERE `name`="test_user"'; if( ($r = $db->query($q)) === FALSE ) trigger_error('SELECT error',E_USER_ERROR); else { list( $id, $stamp ) = $r->fetch_row(); echo $id .' - '. $stamp; $r->free(); } ?> Output 10 - 2011-08-14 06:06:32 Quote Link to comment https://forums.phpfreaks.com/topic/247763-how-to-convert-string-into-datetime-stamp-in-php/#findComment-1272325 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 Duplicate threads merged to this one. Quote Link to comment https://forums.phpfreaks.com/topic/247763-how-to-convert-string-into-datetime-stamp-in-php/#findComment-1272331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.