Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP insert current date time in sql server via sqlsrv drivers

Till now I have tried

$sql = "INSERT INTO table-name(col-name) values (cureent_timestamp)";

and have set the column data type as datetime , I get some values but no date or time.

like image 618
Zerocode Avatar asked Sep 01 '26 13:09

Zerocode


2 Answers

GETDATE() should give you the current date:

$sql = "INSERT INTO db-name(col-name) VALUES (GETDATE())";
like image 155
Mureinik Avatar answered Sep 04 '26 03:09

Mureinik


I solved it using datetime data type for the column-

date_default_timezone_set('Europe/Paris');      //Don't forget this..I had used this..just didn't mention it in the post

$datetime_variable = new DateTime();
$datetime_formatted = date_format($datetime_variable, 'Y-m-d H:i:s');

$sql = "UPDATE table-name SET col-name = '$time_formatted' WHERE ---";
$sql = sqlsrv_query($connection,$sql);

However I am not able to get the perfect datetime. I am getting the next day's date. I am on it! Thanks all for the help! Solved that as well, just need a minor adjustment to the timezone of the computer.

like image 27
Zerocode Avatar answered Sep 04 '26 04:09

Zerocode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!