过滤上个月和当前年度的记录

时间:2013-05-12 23:18:34

标签: php mysql

我需要这个查询的帮助,这个工作正常,问题是它从ingresos返回我的所有内容。我只需要检索上个月和当前年份的记录。

我已经尝试了INTERVAL 1 MONTH,但它根据今天的日期返回了我一个月。 我需要上个月的记录而不计算从第一个到最后一个日期。

这是首次加载页面的时间:

SELECT * from ingresos
where fechaReporteING < DATE_SUB(CURDATE(),INTERVAL DAYOFMONTH(CURDATE()) DAY)

,这是当用户选择特定月份时:

SELECT * from ingresos
where fechaReporteING < DATE(CONCAT(YEAR(CURDATE()),"-",'.$_POST['mes'].',"-","0"))

1 个答案:

答案 0 :(得分:0)

这对我有用。它使用PHP根据用户选择的月份计算日期。

<?php
//Get your users month.
$month = isset($_GET['mes'])? $_GET['mes'] : "04";
//Starting date string `current year`-`user month`-`first day`
$startDateText = date('Y') . "-" . $month . "-01";

//Add one month to the starting date.
$startDate = date_create($startDateText);
$endDate = date_add($startDate, date_interval_create_from_date_string('1 month'));

//String representation of the date
$endDateText = date_format($endDate, 'Y-m-d');

echo "<br><br>Search for records on or after $startDateText but before $endDateText.<br><br>";

$query = "SELECT * FROM ingresos
    WHERE fechaReportING >= '$startDateText' && fechaReportING < '$endDateText' ";

$allData = mysqli_query($db_conx, $query) or printf(mysqli_error($db_conx));
$totalDataRows = mysqli_num_rows($allData);
....
....
?>
相关问题