Mysql每月获取记录或数据

时间:2018-01-10 07:50:48

标签: mysql mysqli

有没有办法编写MySQL查询以按月获取记录,例如:

从当月开始:

----- Jan -----
Record 1
Record 2
----- Feb -----
Record 1
Record 2
----- March-----
Record 1
Record 2

我的db表中有开始和结束时间戳和start_date,end_date(MM / DD / YYYY格式)列。

1 个答案:

答案 0 :(得分:0)

通过获取月份的开始日期和结束日期来解决

$month_start_time = strtotime(date('Y-m-01', $timestamp)." 00:00:00"); 
$month_end_time   = strtotime(date('Y-m-t', $timestamp)." 23:59:59"); 
$sdate = date('d', $month_start_time); 
$edate = date('d', $month_end_time);
$j=0; 
for($k=$sdate; $k <= $edate; $k++){
  $start_date = $k.date('/m/Y', $month_start_time);        
  $stimestamp = strtotime(date('Y/m', $month_start_time).'/'.$k." 00:00:00"); 
  $end_date   = strtotime(date('Y/m', $month_start_time).'/'.$k." 23:59:59"); 
  //then pass in mysql query
}