我需要从给定的日期范围中提取mysql表中的每月数据。 这是php代码:
$start = (new DateTime('2015-01-28'));
$end = (new DateTime('2015-12-12'));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
$mth = $dt->format( "m" );
$dateObj = DateTime::createFromFormat('!m', $mth);
$mm = $dateObj->format('F');
$yr = $dt->format( "Y" );
$sql = 'SELECT id FROM mytable WHERE MONTH(date) = '.$mth.' AND YEAR(date) = '.$yr;
$res = $conn->query($sql);
$rec = $res->num_rows;
}
这样做,我得到了1月和12月整个月的数据。如何确保选择所选日期之后或之后的记录?
是否可以快速了解两个日期范围之间的月数?
答案 0 :(得分:0)
在SQL中,您可以使用此查询:
Select *
from table name
where CAST(dates AS date) BETWEEN '11/2/2014' and '12/5/2014'