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);
Related
I am using PHP code to update week ending date which is on a different day for each subdomain. Each subdomain date range varies.
My current code uses a while loop to update the week end date. This is very slow and I am looking for ideas to optimise it.
Here is my current code:
// Loop through and assign week end date to each user activity.
$nextWeek = $startDate; // initialise while loop condition
while ($nextWeek <= $endDate) {
$lastWeek = $nextWeek;
$nextWeek = Carbon::parse($lastWeek)->addWeek(1)->format('Y-m-d');
// update activity date to week end date
$query = 'UPDATE r_active_user_activities
SET week_end_date = "' . $nextWeek . '"
WHERE (activity_date > "' . $lastWeek . '"
AND activity_date <= "' . $nextWeek . '"
AND subdomain="' . $subdomain->subdomain . '" );';
$db->statement($query);
}
Just append all those update queries in a php variable ($query) and execute that outside the while loop. something like this.
$nextWeek = $startDate; // initialise while loop condition
$query = '';
while ($nextWeek <= $endDate) {
$lastWeek = $nextWeek;
$nextWeek = Carbon::parse($lastWeek)->addWeek(1)->format('Y-m-d');
// update activity date to week end date
$query .= 'UPDATE r_active_user_activities
SET week_end_date = "' . $nextWeek . '"
WHERE (activity_date > "' . $lastWeek . '"
AND activity_date <= "' . $nextWeek . '"
AND subdomain="' . $subdomain->subdomain . '" );';
}
$db->statement($query);
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';
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);
}
I am trying to display data in a table according to dates and currently am using the following code.
if($_GET){
$datefrom = $_GET['datefrom'];
$dateto = $_GET['dateto'];
$date0 = date('Y-m-d', strtotime($datefrom . " +0 days"));
$date1 = date('Y-m-d', strtotime($datefrom . " +1 days"));
$date2 = date('Y-m-d', strtotime($datefrom . " +2 days"));
$date3 = date('Y-m-d', strtotime($datefrom . " +3 days"));
$date4 = date('Y-m-d', strtotime($datefrom . " +4 days"));
}
Then I am querying my database repeatedly using the following statements
$stmtHR0 = $db->query("SELECT * FROM table WHERE Form_Date = '$date0' AND State = 'HR'");
$numHR0 = $stmtHR0->rowCount();
$stmtHR1 = $db->query("SELECT * FROM table WHERE Form_Date = '$date1' AND State = 'HR'");
$numHR1 = $stmtHR1->rowCount();
$stmtHR2 = $db->query("SELECT * FROM table WHERE Form_Date = '$date2' AND State = 'HR'");
$numHR2 = $stmtHR2->rowCount();
$stmtHR3 = $db->query("SELECT * FROM table WHERE Form_Date = '$date3' AND State = 'HR'");
$numHR3 = $stmtHR3->rowCount();
$stmtHR4 = $db->query("SELECT * FROM table WHERE Form_Date = '$date4' AND State = 'HR'");
$numHR4 = $stmtHR4->rowCount();
"HR" is just one of the states and I have 30 states to perform these queries on. The code works but I am wondering if there is a better way of doing this and I would be grateful if experts can help me on this. Please forgive me as I am still learning PHP.
This can be done with one query. Assuming From_Date field is DATE field type:
if ($_GET) {
$dateF = date('Y-m-d', strtotime($_GET['datefrom']));
$dateT = date('Y-m-d', strtotime($_GET['dateto']));
$sql = "
SELECT `From_Date`, COUNT(*) AS `count_num`
FROM `table`
WHERE `From_Date` BETWEEN '$dateF' AND '$dateT' AND `State` = 'HR'
GROUP BY `From_Date`
ORDER BY `From_Date` ASC
";
// db fetch all
}
try this code :
actually you can use array for date
$date = array();
$date[0] = date('Y-m-d', strtotime($datefrom . " +0 days"));
$date[1] = date('Y-m-d', strtotime($datefrom . " +1 days"));
$date[2] = date('Y-m-d', strtotime($datefrom . " +2 days"));
$date[3] = date('Y-m-d', strtotime($datefrom . " +3 days"));
$date[4] = date('Y-m-d', strtotime($datefrom . " +4 days"));
for($i = 0; $i<count($date); $i++{
$query_temp[] = "(
SELECT COUNT(*) FROM table WHERE Form_Date = '" . $date[$i]. "' AND State = 'HR'
) AS count" . $i;
}
$query = "SELECT " . implode(", ",$query_temp);
$stmtHR4 = $db->query($query);
hope this code can solve you problem
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> . "'";