Skip to content
Advertisement

how to display data on 2 different date ranges

I have the following date data, which is used to display MySQL data parameters

$tgaw=date("Y-m-10");
$tgak=date("Y-m-09", strtotime("+1 months"));

and I have the following query:

$querylembur = "SELECT SUM(total) as subtotal FROM lembur WHERE tgl between ".$tgaw." 
AND ".$tgak." AND staff =".$row['nama'];
$rslembur=mysqli_query($con,$querylembur);
$rowlembur=mysqli_fetch_array($rslembur);

The result I want is that the program will display data from date :$tgaw to date: $tgak

Advertisement

Answer

You should be using prepared statements here, and also use the MySQL functions to add one month to the upper date in the range.

$sql = "SELECT SUM(total) AS subtotal FROM lembur WHERE tgl BETWEEN ? AND DATE_ADD(?, INTERVAL 1 MONTH) AND staff = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("sss", $tgaw, $tgaw, $row['nama']);
$stmt->execute();
// fetch here
$stmt->close();
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement