dongwenhui8900 2009-10-28 07:12 采纳率: 0%
浏览 11
已采纳

mysql查询使用年份的日期

I have a problem for querying records into mysql

explanation below The user could pick a date to assign in variable fromdate and todate

example.

$fromdate = 2008/01/01
$todate = 2009/10/31

In above case, i have to loop and query with the ff:

SELECT * FROM table where date BETWEEN '2008/01/01' AND '2008/12/31';

second loop

SELECT * FROM table where date BETWEEN '2009/01/01' AND '2009/10/31';

if $fromdate and $todate is more than one year then I have to limit my query per year. But if less than one year then use $fromdate and $todate in the query.

Thanks in advance

Tirso

  • 写回答

3条回答 默认 最新

  • douzi1986 2009-10-28 07:30
    关注

    You can use something like this to calculate the date.

    $d1 = new DateTime($todate);
    $d2 = new DateTime($fromdate);
    $d2->modify("+1 year");
    $min = min($d1, $d2);
    $todate = $min->format('Y/m/d');
    

    If you want to paginate the results, you will have to modify $fromdate on each page before doing the $todate calculation. For example, if you are on the second page, first load $fromdate into a DateTime object and add one year, if you are on the third page, add two years, etc.

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

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能