Using Mysqli bind_param with date and time columns?


Question

How do you insert data into a MySQL date or time column using PHP mysqli and bind_param?

1
49
4/30/2009 7:51:23 AM

Accepted Answer

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();

79
4/30/2009 8:10:26 AM

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();

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon