douchuilai2355 2013-10-29 23:35
浏览 111
已采纳

使用mysql中的时间戳选择2周前的记录

I have records in a table that are stored using php's strtotime function. What I need and don't have any experience with is querying the database for all records within 2 weeks.

I'm sorry I have no code to show for as using timestamps is all so new to me.

  • 写回答

2条回答 默认 最新

  • donglu9825 2013-10-29 23:42
    关注

    If your date is stored as a timestamp, it's easy enough to take two weeks off it:

    select
        *
    from
        yourTable
    where
        dateField<=UNIX_TIMESTAMP()
        and dateField>=(SELECT UNIX_TIMESTAMP()- 60*60*24*14)
    

    As these are all in seconds, just subtract 60 seconds x 60 minutes x 24 hours x 14 days and you have your required condition.

    FYI: A timestamp is simply the number of seconds that have passed since the unix epoch so addition or subtraction is really rather simple. You can also use PHP's mktime to generate a timestamp if you want a particular date.

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

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误