How to convert 12 to 24 in MySQL opencart - php

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

Related

Merge returns empty

There was a problem with the request. Any ideas?
The situation is as follows, there is a request
MERGE ore as T_Base USING ( SELECT
date
,sec
,shift
,id_ore
FROM ore
WHERE date = '2022-9-20'
AND sec = '2'
AND shift = '1') AS T_Source
ON T_Base.date = T_Source.date
AND T_Base.sec = T_Source.sec
AND T_Base.shift = T_Source.shift
AND T_Base.id_ore = T_Source.id_ore
WHEN MATCHED
THEN UPDATE
SET date = T_Source.date
,sec = T_Source.sec
,shift = T_Source.shift
,id_ore = '1'
WHEN NOT MATCHED
THEN INSERT (date, sec, shift, id_ore)
VALUES (T_Source.date, T_Source.sec, T_Source.shift, '1');
But for some reason it returns "0 rows affected".
Table structure:
[id] [int] IDENTITY(1,1) NOT NULL,
[date] [datetime] NULL,
[sec] [int] NULL,
[shift] [int] NULL,
[id_ore] [int] NULL
Values in the table:
id date sec shift id_ore
11637 2022-09-10 00:00:00.000 1 1 13
11636 2022-09-09 00:00:00.000 1 2 13
etc
Expected Result
When updated, no change
id date sec shift id_ore
11637 2022-09-10 00:00:00.000 1 1 13
1.1) When update, with changes (shift and id_ore updated )
id date sec shift id_ore
11637 2022-09-10 00:00:00.000 1 2(change) 27(change)
When new values are inserted:
id date sec shift id_ore
11638 2022-09-20 00:00:00.000 1 1 1
Sorry for this post. I misunderstood the MERGE construction. If there is no data in any of the tables, the operation will not be performed, since there will be nothing to match
If suddenly, someone encountered a similar, came to the following solution:
IF EXISTS (SELECT
date
,sec
,shift
FROM ore
WHERE date = '" . $y . "-" . $m . "-" . $d . "'
AND sec = '" . $sec . "'
AND shift = '" . $shift. "')
UPDATE ore
SET id_ore = '" . $nam_ryd . "'
WHERE date = '" . $y . "-" . $m . "-" . $d . "'
AND sec = '" . $sec . "'
AND shift = '" . $shift. "'
ELSE
INSERT ore (date, sec, shift, id_ore)
VALUES ('" . $y . "-" . $m . "-" . $d . "', '" . $sec . "', '" . $shift. "', '" . $nam_ryd . "')

DATE_ADD add month and day interval

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

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

strtotime february bug

Okay, so I do understand that if I am running the strtotime function on a day that is the 29th or higher I will incur the February bug and get a result for March instead.
Also I get that if I set the date to Feb 1st, I can avoid the issue.
But here is the problem. I am rolling through the last 12 months of records to generate sales/billing numbers for tracking. How do I ask for all records in February when my loop looks like this?
setlocale( LC_MONETARY, 'en_US' );
$i = 0;
while( $i <= 11 ) {
$select = "SELECT * FROM `my_table` " .
"WHERE YEAR( billing_date ) = '" . date( 'Y', strtotime( -$i . ' month' )) . "' " .
" AND MONTH( billing_date ) = '" . date( 'm', strtotime( -$i . ' month' )) . "'";
$result = mysql_query( $select );
$num_rows = mysql_num_rows( $result );
$sales = 16 * $num_rows;
echo "<p align='center'>";
echo "Sales for " . date( 'M, Y', strtotime( '-' . $i . ' month' ) ) .
"&nbsp" . money_format( '%i', $sales );
echo "</p>";
$i++;
}
How do I avoid the February bug? Would it be an if statement? What would that look like?
Here is a pure SQL solution (if I correctly understand the scenario):
SELECT
YEAR(billing_date) AS billing_year,
MONTH(billing_date) AS billing_month,
16 * COUNT(*) AS sales /* Why x16?!!! */
FROM my_table
GROUP BY YEAR(billing_date), MONTH(billing_date)
This will calculate the sales directly in SQL, so in PHP you just need a display loop.
How do I ask for all records in February...
SELECT * FROM table WHERE MONTH(billing_date) = 2
February bug? It's not a bug. Normalizing dates is a (useful) feature! :-)
If you want the last day in February, ask for the 0th of March.
I don't know the "february bug", but you could try to use MySQL to subtract 1 month. Check
SELECT DATE_SUB(billing_date, INTERVAL 1 MONTH) FROM my_table
=> Manual

convert input date to mysql readable format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP/MySQL - Format date/time
I have input field (name:day1 and varchar) to enter date in my form. It attached with a jquery calendar and picks the date in the format "D, dd M, yy" (I WANT TO PICK LIKE THIS AND CANT CHANGE FORMAT). I want to convert and save into mysql date format into table. How is this possible?
$insert_query = 'insert into '.$this->tablename.'(
venue,
day1,
day2,
day3,
day4,
day5,
day6,
day7,
day8,
day9,
day10,
city,
contactperson,
)
values
(
"' . $this->SanitizeForSQL($formvars['venue']) . '",
"' . $this->SanitizeForSQL($formvars['day1']) . '",
"' . $this->SanitizeForSQL($formvars['day2']) . '",
"' . $this->SanitizeForSQL($formvars['day3']) . '",
"' . $this->SanitizeForSQL($formvars['day4']) . '",
"' . $this->SanitizeForSQL($formvars['day5']) . '",
"' . $this->SanitizeForSQL($formvars['day6']) . '",
"' . $this->SanitizeForSQL($formvars['day7']) . '",
"' . $this->SanitizeForSQL($formvars['day8']) . '",
"' . $this->SanitizeForSQL($formvars['day9']) . '",
"' . $this->SanitizeForSQL($formvars['day10']) . '",
"' . $this->SanitizeForSQL($formvars['city']) . '",
"' . $this->SanitizeForSQL($formvars['contactperson']) . '",
)';
day 1 to day 10 are the dates want to convert...
Try this on your date input:
$date = date('Y-m-d H:i:s', strtotime($_GET['date_input']));
As long as strtotime() picks up the right timestamp from your input, you should be ok. Don't forget to escape it as well.
or in the sql statement you can do that:
$insert_query = 'insert into '.$this->tablename.'(
venue,
day1,
day2,
day3,
(.......) )
values
("' . $this->SanitizeForSQL($formvars['venue']) . '",
STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day1']) . '","%W, %d %M, %y"),
STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day2']) . '","%W, %d %M, %y"),
STR_TO_DATE("' . $this->SanitizeForSQL($formvars['day1']) . '","%W, %d %M, %y"),
(.......) )';
The "%W, %d %M, %y" bit must be in the same format as the date you get from your calendar...
To check all date functions and dates format see these links:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
I hope that helps...

Categories