douzhi7070 2015-01-30 07:26
浏览 195
已采纳

如何使用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 2015-01-30 07:32
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?