Unable to get TIME_FORMAT to show 24-hour time - mysql

Using mySQL 5.6.
I am storing time in my database like this:
15:00:00
(So that's 3:00 pm. This is what is stored in the database.)
When I want to select data based on time, I do something like this:
#x = "03:00 pm"; (This is what is being passed in from my time widget.)
SELECT * FROM mytable
WHERE start = TIME_FORMAT(#x, '%H:%i:%s');
But this returns 03:00:00, so no match.
(Remember 15:00:00 is in the database, so I need TIME_FORMAT to change 3:00 pm to 15:00:00, not 03:00:00.)
I have tried all of the following:
SELECT TIME_FORMAT('03:00 pm', '%H:%i:%s')
SELECT TIME_FORMAT('03:00 pm', '%h:%i:%s')
SELECT TIME_FORMAT('03:00 pm', '%T')
And NONE of them give me the 15:00:00 that I need.
Here is what the manual says:
This is used like the DATE_FORMAT() function, but the format string may
contain format specifiers only for hours, minutes, seconds, and microseconds.
Other specifiers produce a NULL value or 0.
And here are a number of sites showing examples and the tables that show what formatting symbols will work with the TIME_FORMAT function:
https://www.techonthenet.com/mysql/functions/time_format.php
http://www.java2s.com/Tutorial/MySQL/0280__Date-Time-Functions/TIMEFORMATtimeformat.htm
http://www.w3resource.com/mysql/date-and-time-functions/mysql-time_format-function.php
http://www.mysqltutorial.org/mysql-time/
So, everything here is telling me that what I'm doing should work.
What am I not getting here?

You can use STR_TO_DATE():
select str_to_date('03:00 pm','%h:%i %p')

The following:
SELECT TIME_FORMAT('03:00 pm', '%H:%i:%s')
SELECT TIME_FORMAT('03:00 pm', '%H:%i:%S')
SELECT TIME_FORMAT('03:00 pm', '%T')
are correct to storing your database like your wanting.
maybe you should try to change the 'date' parameter.

Related

How to Convert date day time format to date only. example "07-13-2020 Mon 10:13:32 am" to "07-20-2020" in sql

Good day,
I am trying to fetch range of data in my database but i cant get any result when i try to query like this.
SELECT * FROM `doc_history` WHERE date BETWEEN '07-13-2020 Mon 10:13:32 am' AND '07-20-2020 Mon 01:24:28 pm'
but if i try only to search specific date. it normally show the data. but when it comes to RANGE SEARCH, it show nothing.
I am trying to convert this date format to a simplified date format "07-20-2020 Mon 01:24:28 pm" to "07-20-2020".
I tried others formula but still no result.
You should valid MySQL timestamp/datetime literals:
SELECT *
FROM doc_history
WHERE STR_TO_DATE(date, '%m-%d-%Y %a %h:%i:%s %p')
BETWEEN '2020-07-20 13:24:28' AND '2020-07-20 13:24:28';
But note that you are currently not really searching for a range, but rather a point in time. So the above query I wrote is equivalent to:
SELECT *
FROM doc_history
WHERE STR_TO_DATE(date, '%m-%d-%Y %a %h:%i:%s %p') = '2020-07-20 13:24:28';
If you want a meaningful range, then the start and end values should be different points in time.

Adding one hour on date time, with AM PM in count

I want to get the data between 2 dates, I have a procedure that takes fromDate as input, for example like if I give this date for it 2022/02/02 9:10:36 PM, and I want to add 3 hours to the ToDate, so it should be like that: 2022/02/02 12:10:36 AM
I tried DATE_ADD('2022/02/02 9:10:36 PM', INTERVAL +3 Hour) but it didn't work it gave it PM and it should be AM since its 12 AM after adding 3 hours to 9 pm.
SUMMARY: I want to get data with 3 hour range in procedure 2022/02/02 9:10:36 ((PM)) to 2022/02/02 12:10:36 ((AM)), the procedure take the from date and i want to add three hours with AM PM in count
First you have to convert the string date to an actual datetime type and then add the hours. Use str_to_date() which allows you to tell the conversion process what the string looks like so it can complete the conversion correctly.
Here is a simple demo
SELECT STR_TO_DATE('2022/02/02 9:10:36 PM', '%Y/%m/%d %h:%i:%s %p') original,
DATE_ADD( STR_TO_DATE('2022/02/02 9:10:36 PM', '%Y/%m/%d %h:%i:%s %p') , INTERVAL +3 Hour) new_dt;
RESULT
original new_dt
2022-02-02 21:10:36 2022-02-03 00:10:36
It is best to store dates and times in the appropriate data type in the first place so functions work correctly automatically. If your location requires a specific presentation of the date, do that conversion in the presentaion layer not the storage layer

Trouble with DATE_FORMAT and STR_TO_TIME with AM/PM

I need to get the 24 hour time of a string, but I can only get the 12 hour for some reason using Mysql.
SELECT STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %T');
+-----------------------------------------------------+
| STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %T') |
+-----------------------------------------------------+
| 2018-03-13 09:28:07 |
+-----------------------------------------------------+
I have tried a variety of methods and thought it was working correctly, which it does, before noon....
I am trying to use it to limit the returned results to only things that have changed since the last time I ran the query.
%T is for time in 24 hour notation, so STR_TO_DATE is ignoring the PM/AM part of your time. You need to use %r. See the manual for details.
You need to convert it to datetime with time zone then use date format %T.
select DATE_FORMAT(STR_TO_DATE('3/13/2018 9:28:07 PM', '%m/%d/%Y %r'), '%T')
21:28:07

Convert MySQL Unix Time Stamp to Human Readble Form

Appreciate that this topic has been covered many times and I have tried all the combinations I can find without success.
The following timestamp is an example of that returned when using rpt_default_day.time_stamp:
1474502400000
If I put this time stamp into the following website it returns the correct date and time:
http://www.epochconverter.com/
Below are some examples of queries I have been using:
DATE_FORMAT(FROM_UNIXTIME('rpt_default_day.time_stamp'), '%e %b %Y') AS 'Date',
FROM_UNIXTIME('rpt_default_day.time_stamp') AS 'Date',
FROM_UNIXTIME(UNIX_TIMESTAMP('rpt_default_day.time_stamp')) AS 'Date',
Problem is whatever I do I'm always getting returned the epoch time of:
'1970-01-01 01:00:00.000000'
Appreciate any help in advance.
Remove three zeros from your string and you're good to go.
A proper format values should be passed as a parameter to "FROM_UNIXTIME" function :
mysql> SELECT UNIX_TIMESTAMP(NOW()) as ts,
FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()), '%Y %D %M %h:%i:%s %x') as f_tm;
+------------+-----------------------------------+
| ts | f_tm |
+------------+-----------------------------------+
| 1474755927 | 2016 25th September 01:25:27 2016 |
+------------+-----------------------------------+
1 row in set (0.06 sec)
possible format values are specified in the Mysql Documentation

mysql query - format date on output?

In my table, dates are stored like this: 2011-03-03T13:30:00
I'm trying to output dates like this: March 3, 2011 1:30 PM
I'd much rather work it into the query rather than use php to format it, but I'm having some difficulty doing that. Trying various iterations of DATE_FORMAT, but it's not giving me what I want, maybe because of the way it's being stored?
You basically have two different operations you may need to perform when handling dates: date to string and vice versa. The functions you can use are DATE_FORMAT() and STR_TO_DATE(). Full reference can be found in the manual.
Usage example:
SELECT
DATE_FORMAT(CURRENT_TIMESTAMP, '%d/%m/%Y %H:%i:%s'),
STR_TO_DATE('31/12/2001 23:55:00', '%d/%m/%Y %H:%i:%s')
If your dates are not real dates but strings, you'll need to convert twice: from string to date and again from date to string:
SELECT
STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'),
DATE_FORMAT(STR_TO_DATE('2011-03-03T13:30:00', '%Y-%m-%dT%H:%i:%s'), '%M %e, %Y %l:%i %p')
Use DATE_FORMAT:
DATE_FORMAT(date, "%M %e, %Y %h:%i %p")
The MySQL date storage format is actually YYYY-MM-DD, but using the str_to_date() and date_format() functions you can accept and generate any date format required.
select DATE_FORMAT(DateTable.MyDate,'%d %b %y')
from DateTable
would return
04 Nov 08
You should really use a DATETIME field for such things (and clean the input on the way in) rather than having to sort this out at the point of output.
Irrespective, you can simply use the DATE_FORMAT function to re-format your field into the format you require, although this might produce some un-expected results on a VARCHAR or CHAR field. (If so, you'll have to use STR_TO_DATE or failing that some various string functions to extract the date bits you require.)