Using Mysqli bind_param with date and time columns?

Solution 1:

Like any other string

$stmt = $mysqli->prepare('insert into foo (dt) values (?)');
$dt = '2009-04-30 10:09:00';
$stmt->bind_param('s', $dt);
$stmt->execute();

Solution 2:

Timestamps in PHP are integers (the number of seconds from the UNIX epoch). An alternative to the above is to use an integer type date/time parameter, and the MySQL functions FROM_UNIXTIME and UNIX_TIMESTAMP

$stmt = $mysqli->prepare("INSERT INTO FOO (dateColumn) VALUES (FROM_UNIXTIME(?))");
$stmt->bind_param("i", $your_date_parameter);
$stmt->execute();

Solution 3:

For the current date/time you can use the MySQL standard routine. You do not have to prepare that.

$query  = "INSERT INTO tablename ";
$query .= "VALUES(?,?,?,NOW()) ";
$preparedquery = $dbaselink->prepare($query);
$preparedquery->bind_param("iii",$val1,$val2,$val3);