PHP - Insert date time object in the type of DateTime column in database

Discuss coding issues, and scripts related to PHP and MySQL.
PloMar
Posts:48

PHP - Insert date time object in the type of DateTime column in database

I want to insert a DateTime object in database (created in PHP) where the column type is DateTime. How can I do?
I am using this code:

Code: Select all

$cdate = new DateTime('now');
$cd = $cdate->format('d/m/Y h:i:sa');
$udate = new DateTime('72 hours');
$ud = $udate->format('d/m/Y h:i:sa');
$sql = "INSERT INTO table (name, cd, ud) VALUES ('some_name', '$cd', '$ud')";

MarPlo Posts:186
Use the "Y-m-d H:i:s" date-time format in PHP, it is the format of the DateTime column in MySQL.

Code: Select all

$cdate = new DateTime('now');
$cd = $cdate->format('Y-m-d H:i:s');

Similar Topics