DATE_ADD add month and day interval - php

So PHP does not accept doing something along the lines of:
$sql = "SELECT DATE_ADD('" . $this->db->escape($data['regpro_buy_date']) . "', INTERVAL " . $year_flag . " YEAR + INTERVAL " . $mon_flag . " MONTH) AS warranty_date; ";
but if I do:
SELECT NOW() + INTERVAL 2 YEAR + INTERVAL 2 MONTH
in MYSql it works fine. I am unsure of how to get it to where month and year are working together.
currently month and year are defined as:
$year_flag = 2;
$mon_flag = 3;

Hehe, nevermind, I was able to figure it out.
$sql = "SELECT DATE_ADD(DATE_ADD('" . $this->db->escape($data['regpro_buy_date']) . "', INTERVAL " . $year_flag . " YEAR), INTERVAL " . $mon_flag . " MONTH) AS warranty_date; ";

Related

Bulk MySQL update instead of PHP While loop

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);

Pull a subset of data from an array

Is this possible to do?
I have a query that is selecting all records from a table, which the results is based on a time field. This is a schedule of games for 17 weeks. What I am trying to do is create an array of just the current weeks games that have expired. In my code below, every expired game shows in the array. Is it possible to just create this separate array to only include the current week games? I still need to capture all the data, but am trying to create an array of data just for the current week as seen in the row expired portion of the code.
$latestweek = getCurrentWeek(); //gives the current week
for($wk=1;$wk<=17;$wk++){
$games = array();
$sql = "select s.*, (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "') as expired ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "where s.weekNum = " . $wk . " ";
$sql .= "order by s.gameTimeEastern, s.gameID";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$e = 0;
while ($row = $query->fetch_assoc()) {
$games[$row['gameID']]['gameID'] = $row['gameID'];
$games[$row['gameID']]['homeID'] = $row['homeID'];
$games[$row['gameID']]['visitorID'] = $row['visitorID'];
$games[$row['gameID']]['sWinner'] = $row['result'];
$games[$row['gameID']]['startTime'] = $row['gameTimeEastern'];
if ($row['expired']){
$survGamesExpired_h[$e]=$row['homeID'];
$survGamesExpired_v[$e]=$row['visitorID'];
}
$expiredTeams = array_merge((array)$survGamesExpired_h,(array)$survGamesExpired_v);
}
}

How to convert 12 to 24 in MySQL opencart

I am trying to take insert hours and minutes from MySQL in OpenCart
instead updating the input value its update 12:00 AM for all the time when i click for save individually
here is the query
$this->db->query("UPDATE " . DB_PREFIX . "delivery_time SET delivery_time_name = '" . $this->db->escape($data['delivery_time_name']) . "', delivery_time_from = 'TIME( STR_TO_DATE( " . $this->db->escape($data['delivery_time_from']) . ", \"%h:%i %p\" ) )', delivery_time_to = '" . $this->db->escape($data['delivery_time_to']) . "', status = '" . (int)$data['status'] . "' WHERE delivery_time_id = '" . (int)$delivery_time_id . "'");
but same working on phpmyadmin
UPDATE `oc_delivery_time` SET `delivery_time_name`="Slot2",`delivery_time_from`=TIME( STR_TO_DATE( "9:30 AM", "%h:%i %p" ) ),`delivery_time_to`=TIME( STR_TO_DATE( "12:00 PM", "%h:%i %p" ) ),`status`="1" WHERE `delivery_time_id`="2"
Use MySQL STR_TO_DATE or interval
STR_TO_DATE('1:00 PM', ' %h:%i')+ interval 12 hour
or try this query :-
Select STR_TO_DATE('1:00 PM', ' %h:%i')+ interval 12 hour
You should use %H instead of %h, wrong pattern
h stands for 00 to 12
H for 00 to 23
http://www.techonthenet.com/mysql/functions/str_to_date.php

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> . "'";

Categories