Reputation: 259
I want to insert a DateTime object in database where the column type is DateTime. How can I achieve this?
I am using this code:
$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')
$insert = "insert into `winpc_user(mac_address,reg_date,updated_date,status,processor_name,ram_size,os_Name, os_Bits) values('$mac','$cdate','$udate','$stat','$proName','$rSize','$osName','$osBits')"
Upvotes: 4
Views: 820
Reputation: 41903
Same as the comment above, DATETIME
's format is:
YYYY-MM-DD HH:MM:SS
Quite straightforward to follow using date()
's format function, it'll share the same with the ->format()
:
->format('Y-m-d H:i:s');
Sidenote: Of course, this needs to be quoted as well on insertion.
As an alternative, you could also use MySQL functions to achieve the same goal:
NOW()
DATE_ADD(NOW(), INTERVAL 72 HOUR)
Upvotes: 4