doujiu3768 2013-10-16 18:36
浏览 44
已采纳

如何从特定日期起30天内获得确切日期? [重复]

This question already has an answer here:

I have mysql date like this 2013-10-16 17:44:13 and I need to get +30 days from there. I can't use eg. 10th ( month ) and change it to 11th ( month ) as this may be 31 day or 29 depending on the month

I can only think of converting 2013-10-16 17:44:13 to timestamp than + 30*24*60*30, and than this new timestamp back to mysql format

Is there a better way?

</div>
  • 写回答

2条回答 默认 最新

  • dty98339 2013-10-16 18:39
    关注

    You can use strtotime for this:

    $date = date('Y-m-d H:i:s', strtotime($date . ' +30 days'));
    

    or do it directly in MySQL using DATE_ADD:

    SELECT DATE_ADD(`date`, INTERVAL 30 DAY) as `date` FROM `table`
    

    If you run a newer version of MySQL, you don't need to use DATE_ADD:

    SELECT (`date` + INTERVAL 30 DAY) as `date` FROM `table`
    

    Please note that while strtotime is smart enough, MySQL requires you to use DAY. Not DAYS.

    Edit: I am unable to find any proof of DATE_ADD being needed in older versions, but I swear that I've heard it somewhere. Take it with a grain of salt and use whatever method you prefer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)