4

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')"
2

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.