Zerocode
Zerocode

Reputation: 71

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.

Upvotes: 3

Views: 20076

Answers (3)

Zerocode
Zerocode

Reputation: 71

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.

Upvotes: 4

Antony D'Andrea
Antony D'Andrea

Reputation: 1004

If you are using PHP (your code isn't very clear where you are getting cureent_timestamp from which is spelt wrong by the way). You may want to do this,

$current_timestamp = date('Y-m-d H:i:s');
$sql = "INSERT INTO db-name(col-name) values ('{$current_timestamp}')";

But using MySQL functions that other people have answered are probably better in your case, just wanted to provide an alternative.

Upvotes: 2

Mureinik
Mureinik

Reputation: 311188

GETDATE() should give you the current date:

$sql = "INSERT INTO db-name(col-name) VALUES (GETDATE())";

Upvotes: 6

Related Questions