douzhi7070
douzhi7070
2015-01-30 07:26

如何使用php在数据库的DateTime列类型中插入日期时间对象

已采纳

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')"
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongtan9066 dongtan9066 6年前

    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)
    
    点赞 评论 复制链接分享

相关推荐