仅从数据库获取上周的结果

时间:2018-09-05 08:20:53

标签: php mysql

我正在使用此php class。它正在帮助我使用数据库。

因此,一个名为vocabulary的表具有名为datetime_uploaded

的行

示例结果: 2018-08-31-11-13-05

现在我只想从这个php类的数据库宽度中获取上周的结果。

我有上周获取服务的功能

function getLastWeekDates()      
       {
          $lastWeek = array();

          $prevMon = abs(strtotime("previous monday"));
          $currentDate = abs(strtotime("today"));
          $seconds = 86400; //86400 seconds in a day

          $dayDiff = ceil( ($currentDate-$prevMon)/$seconds );

          if( $dayDiff < 7 )
          {
              $dayDiff += 1; //if it's monday the difference will be 0, thus add 1 to it
              $prevMon = strtotime( "previous monday", strtotime("-$dayDiff day") );
          }

          $prevMon = date("Y-m-d",$prevMon);

          // create the dates from Monday to Sunday
          for($i=0; $i<7; $i++)
          {
              $d = date("Y-m-d-H-i-s", strtotime( $prevMon." + $i day") );
              $lastWeek[]=$d;
          }

          return $lastWeek;
      }

它给了我结果:

(
    [0] => 2018-08-27-00-00-00
    [1] => 2018-08-28-00-00-00
    [2] => 2018-08-29-00-00-00
    [3] => 2018-08-30-00-00-00
    [4] => 2018-08-31-00-00-00
    [5] => 2018-09-01-00-00-00
    [6] => 2018-09-02-00-00-00
)

,但不知道如何从数据库中获取此信息。

我的php版本是7.x

2 个答案:

答案 0 :(得分:1)

SELECT *
FROM table
WHERE DATE(date + INTERVAL (YEAR(NOW()) - YEAR(date)) YEAR)
      BETWEEN
      DATE(NOW() - INTERVAL WEEKDAY(NOW()) DAY)
      AND
      DATE(NOW() + INTERVAL 6 - WEEKDAY(NOW()) DAY);

答案 1 :(得分:0)

这将返回上周(过去7天)的记录:

SELECT * FROM vocabulary
WHERE datetime_uploaded >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND datetime_uploaded < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY

如果您已经拥有两个日期,则可以使用:

SELECT * FROM vocabulary
WHERE datetime_uploaded BETWEEN "2011-05-15" AND "2011-05-21"