dpkrbe395930 2014-06-02 16:20
浏览 222
已采纳

如何使用date_add将年,月,日,小时,分钟,秒添加到mysql日期?

What is the best way to add to a datetime type column in mysql database? I tried using DATE_ADD but it only works when I add single date/time like:

DATE_ADD(start_date, INTERVAL :dayCount DAY)

This does NOT Work:

DATE_ADD(DATE_ADD(start_date, INTERVAL :dayCount DAY), INTERVAL :hourCount HOUR)

I tried adding microseconds as suggested by some:

$startMicro = strtotime($startDiff->format("%Y-%m-%d %H:%i:%s")) * 1000; // 943920000000
start=DATE_ADD(start, INTERVAL :startMicro MICROSECOND)

I DID THIS IN THE END AND IT WORKS: in PHP: $minutesDiff = (strtotime($start) - strtotime($row->start)) / 60; in SQL STATEMENT: start=DATE_ADD(start, INTERVAL :minutesDiff MINUTE)

Thanks all for the help.

  • 写回答

2条回答 默认 最新

  • dongzu0742 2014-06-02 16:29
    关注

    Convert the total time being added to the smallest increment of time you want to add, so rather than 1 day, 1 hour, you'd do 25 hours. Instead of 1 hour, 1 minute, 1 second, you'd do 3601 seconds, etc.

    You can nest them if desired, but your syntax is incorrect, missing a comma before 2nd INTERVAL, should be:

    DATE_ADD(DATE_ADD(start_date, INTERVAL :dayCount DAY), INTERVAL :hourCount HOUR)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了