how to get dates between dates that user inter - php

How to get all data between to dates (from-dates & to_dates ) that user enter from a html form like. When user enter date like, from (1\1\2019) to (30\1\2019)
I want check if that two date between date in MySQL table
I tried :
$from = $_POST['from'];
$end = $_POST['end '];
$sql = "SELECT * FROM table_name WHERE '" . $from . "' between
from_date and to_date and '" . $end . "' between from_date and
to_date
";
from_date and to_date...col in mysql table

Here, but you should format $from and $end to correct format
"SELECT * FROM table_name WHERE (date_column_name BETWEEN '" . $from . "' AND '" . $end . "') AND (date_column_name2 BETWEEN '" . $from . "' AND '" . $end . "')"
Where table_name is table you want to SELECT and date_column_name is your date column.

Try this,
$from = $_POST['from'];
$end = $_POST['end '];
$sql = "SELECT * FROM table_name WHERE date_column1 >= '" . $from . "' AND date_column2 <= '" . $end . "' ";

First you need to convert the input dates in to database date format
yyyy-mm-dd
$from = date('Y-m-d',strtotime($_POST['from']));
$end = date('Y-m-d',strtotime($_POST['end ']));
$sql = "SELECT * FROM table_name WHERE (date_column_name1 BETWEEN '" . $from . "' AND '" . $end . "') AND (date_column_name2 BETWEEN '" . $from . "' AND '" . $end . "')";
OR
$sql = "SELECT * FROM table_name WHERE (from_date_column >='" . $from . "' AND to_date_column <='" . $end . "')";

Please try this
SELECT * FROM table_name
WHERE from_date >='$from' AND to_date <='$end';
or
SELECT * FROM table_name
WHERE from_date BETWEEN '$from' AND '$end' or to_date BETWEEN '$from' AND '$end';

Related

Trying to add date range to query

I am working in OpenCart and am trying to change this query. I need to change it so that it returns records from that last year(1 year from now) here is my query
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND YEAR(date_added) = YEAR(CURDATE()) GROUP BY channel");
I tried changing it to this but it ddidnt work:
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND date_added BETWEEN CURDATE() - INTERVAL 1 year AND CURDATE()) GROUP BY channel");
Before, consider to sanitize your input data to protect your queries against SQL Injection. Here you can find some content about this topic.
Based on this answer, we can adapt your PHP code to the following:
$query = $this->db->query("SELECT count(*) AS total, channel
FROM `" . DB_PREFIX . "order`
WHERE customer_id = '" . (int)$customer_id . "'
AND order_status_id IN(" . implode(",", $implode) . ")
AND date_added >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
GROUP BY channel");
You got the wrong syntax when calculating the last year.
You can use DATE_SUB.
$query = $this->db->query("SELECT count(*) AS total, channel FROM `" . DB_PREFIX . "order` WHERE customer_id = '" . (int)$customer_id . "' && order_status_id IN(" . implode(",", $implode) . ") AND date_added BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND CURDATE()) GROUP BY channel");

PHP/Mysql: optimize query

I have the following script that retrieve numbers from 2 tables, make a sum, and the value is updated into a 3th table.
$query = "SELECT (SELECT SUM(net_amount) FROM fin_costos WHERE month='1' AND group_of_costos='general' AND year_analysis='2014' ) +
(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='1' AND group_of_costos='general' AND year_analysis='2014') AS total";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result)){$valor_final = $row['total']; }
$query_update="UPDATE fusion_analysis SET jan='$valor_final' WHERE year_for_analysis='2014' AND item_for_analysis='general' AND category='general'";
$result = mysqli_query($mysqli,$query_update);
I need to run the same script for each month of the year. Everything is exaclty the same except the variable 'month' that changes from 1 to 12 and the SET in UPDATE where the value is uploaded for each month ('jan','feb', 'mar'...etc)
I'm currently just copying and pasting the same script changing this few parameters but I believe there is a smarter way to do this in less lines of code I have
See date function of PHP:
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='".date('n')."'"
." AND group_of_costos='general' AND year_analysis='".date("Y")."' ) +"
."(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='".date('n')."'"
. " AND group_of_costos='general' AND year_analysis='".date("Y")."') AS total";
$query_update="UPDATE fusion_analysis"
. " SET `". strtolower(date('M'))."`='$valor_final'"
. " WHERE year_for_analysis='".date("Y")."'"
. " AND item_for_analysis='general'"
. " AND category='general'";
NOTE:
Y - A full numeric representation of a year, 4 digits like 2014
n - Numeric representation of a month, without leading zeros 1 - 12
M - A short textual representation of a month, three letters Jan through Dec
For month as short textual I've used the strtolower function to make it lowercase.
UPDATE
Based on OP comment:
for ($i = 1; $i <= 12; $i++) {
$query = "SELECT (SELECT SUM(net_amount)"
. " FROM fin_costos"
. " WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "' ) +"
. "(SELECT SUM(net_amount) FROM em2_fin_costs WHERE month='" . $i . "'"
. " AND group_of_costos='general' AND year_analysis='" . date("Y") . "') AS total";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_assoc($result);
$valor_final = $row['total'];
$monthName = strtolower(date('M', strtotime(date("Y") . "-" . str_pad($month,2, "0", STR_PAD_LEFT) . "-" . date("01") )));
$query_update = "UPDATE fusion_analysis"
. " SET `" . $monthName . "`=' " . $valor_final . "'"
. " WHERE year_for_analysis='" . date("Y") . "'"
. " AND item_for_analysis='general'"
. " AND category='general'";
mysqli_query($mysqli, $query_update);
}

SQL Query using DATE_ADD for time

Hey guys I've tried to figure this out,
I have
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . $currenttime . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . $currenttime . "'";
I can get the first AND to return, but the second part wont work where i have DATE_ADD, ive tried it by itself to return anything and i cant get it to work!
$current time right now is 06:01:14, its = date(h:i:s);
then in play_datetime it picks up the match_date lists a result, the time in play_datetime is 04:00:00
According to your query :
$currendate have date
and $currenttime have time.
But your column name "play_datetime" suggest that it is having datetime or timestamp .
so if this is right that your query is working but you have to put $currentdatetime instead of $currenttime.
$query = "SELECT URL
FROM Matches
WHERE match_date = '" . $currentdate . "'
AND play_datetime < '" . <timestamp> . "'
AND DATE_ADD(play_datetime, INTERVAL 3 HOUR) > '" . <timstamp> . "'";

Can a variable be used in Mysql query after WHERE

the below query works and returns results
$query = "SELECT * FROM table WHERE District = '" . $var . "' ORDER BY Form_Date DESC";
where as if I substitute the word "District" with a variable, it doesn't work
$query = "SELECT * FROM table WHERE '" . $distvar . "' = '" . $var . "' ORDER BY Form_Date DESC";
what is wrong with this one and how can I make it work?
Remove the quotes surrounding the field you are testing for, or replace them with backticks to save you from the mysql parser mistaking it for a potentially reserved word:
$query = "SELECT * FROM `table` WHERE `" . $distvar . "` = '" . $var . "' ORDER BY Form_Date DESC";
try this :
$query = "SELECT * FROM table WHERE `" . $distvar . "` = '" . $var . "' ORDER BY Form_Date DESC";
or
$query = "SELECT * FROM table WHERE " . $distvar . " = '" . $var . "' ORDER BY Form_Date DESC";

Select data between two dates

I have six dropdown menus for selecting two dates
I have the folowing code, which give me no results no matter which date i choose
$year1 = $_POST['year1'];
$month1 = $_POST['month1'];
$day1 = $_POST['day1'];
$date1 = $year1 . "/" . $month1 . "/" . $day1;
$year2 = $_POST['year2'];
$month2 = $_POST['month2'];
$day2 = $_POST['day2'];
$date2 = $year2 . "/" . $month2 . "/" . $day2;
$result = mysql_query("SELECT * FROM services WHERE date between '%" . $date1 . "%' AND '%" . $date2 . "%' ORDER BY id " );
but if I replace variables $date1 and $date2 in last line with specific date i get the right result.
$result = mysql_query("SELECT * FROM services WHERE date between '2012/10/01' AND '2012/11/12' ORDER BY id " );
Can anyone tell what it wrong with variable $date1 and $date2?
you need to remove the % symbols from your query and just use
$result = mysql_query("SELECT * FROM services WHERE date between '" . $date1 . "' AND '" . $date2 . "' ORDER BY id " );
% is using for wild card search... you have to remove it from your code...
$sql = "SELECT * FROM services WHERE date between '" . $date1 . "' AND '" . $date2 . "' ORDER BY id ";
mysql_query($sql);

Categories