mysql 12 hr to 24 hr time conversion - mysql

How can we convert the time in AM/PM to 24-hrs format. For eg. (1:30 PM) should be converted to (13:30).

Dates / Times are stored in mysql the same way regardless of how they are formatted.
I believe what you want to do is retrieve the date in a specified format.
The DATE_FORMAT() will do this for you.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
%r and %T are 12 hour and 24 hour time formats respectively.

Hi all I think this will help you
select STR_TO_DATE('8:25 PM', '%l:%i %p' )
the result will be
>20:25:00

You could use MySQL's DATE_FORMAT http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

1:=> print( date("H:i:s", strtotime("1:30 pm")) );
output: 13:30:00

This may help:
Table: records
Column date: Type date,
Column hours: Type string (am/pm format)
SELECT
date,
hours,
CONCAT(date, ' ', IF(LOCATE('pm',hours) > 0, ADDTIME(TIME(REPLACE(hours, ' pm','')), '12:00:00'), TIME(REPLACE(hours, ' am',''))))
FROM records

Related

varchar date time comparison issue

I have a legacy table which has a varchar column represent date, format is MM/DD/YYYY (e.g. 01/08/2015). It is not convenient to perform data range selection since it is a varchar (when I use < or > kinds comparison, it goes to varchar/string comparison, which have different results from date comparision).
For example, I want to select only rows which dates are between 01/08/2015 and 01/10/2015. Any smart solution is appreciated, and I cannot change the data type of varchar to date in my existing table.
I am using MySQL Workbench/MySQL.
Varchar dates are evil and they are not real date, the best solution is to use mysql's native date data types.
Since you can't change the datatype you may use str_to_date() function and here how it works
mysql> select str_to_date('01/08/2015','%d/%m/%Y') as d ;
+------------+
| d |
+------------+
| 2015-08-01 |
+------------+
So the query for select would be
select * from table_name
where
str_to_date(date_column,'%d/%m/%Y')
between
str_to_date('01/08/2015','%d/%m/%Y')
and
str_to_date('01/10/2015','%d/%m/%Y')
There are many answers which addresses many different way of converting the string to date.
You may choose whichever is perfect for your need
SELECT * FROM `your_table` WHERE DATE_FORMAT(my_column_with_the_string_date, "%Y-%m-%d") <= '2011-09-30'
DATE_FORMAT can be used to convert your date string to any format: I will use the NOW() function instead of string to list different
formats that are supported
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
The output of the above is:
Nov 04 2014 11:45 PM
11-04-2014
04 Nov 14
04 Nov 2014 11:45:34:243
You can modify your query accordingly
You can cast your dates as strings using STR_TO_DATE:
STR_TO_DATE(yourdatefield, '%m/%d/%Y')
SELECT STR_TO_DATE(got_fired_at, '%m/%d/%Y') BETWEEN ? AND ? FROM firings;
(field/table names guaranteed to have been chosen randomly)
Use MySQL's STR_TO_DATE function to parse the date strings to date objects then do the comparison.

How to parse English string dates in MySQL?

In my SQL database Valid Up To : Thursday, January 01, 2015 3:17 AM is stored as string with column name valid time. Now I want to select data which are updated in last one hour. How can I make a suitable query?
You can parse the string and convert it to a date with STR_TO_DATE(). Parsing options are described here.
SELECT
*
FROM your_table
WHERE STR_TO_DATE(`valid time`, 'Valid Up To : %W, %M %d, %Y %l:%m %p') >= NOW() - INTERVAL 1 HOUR;

How to convert date in MYSQL to correct format

My current date formats are 01/01/2013 .... DD/MM/YYYY
How can I convert them all into MYSQL dates? I'm under The impression they are in the format YYYY-MM-DD
I don't know where to start.
my problem is that the dates are being ordered in the american way whilst they are in british format :(
Use the following query.
update tbl_name set your_field_name= str_to_date(your_field_name, '%d/%m/%Y');
It will update the value of your date from DD/MM/YYYY to YYYY/MM/DD.
Then you can change your filed type to date.
You can extract each part in php and concat the dd, mm, yyyy and save it to the DB
Why not DATE_FORMAT
DATE_FORMAT(date,'%Y-%m-%d')
How to convert date in MYSQL to correct format:
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
or
SELECT t.id, DATEDIFF(STR_TO_DATE(t.carddate, '%m/%d/%Y'), CURDATE)
FROM TABLE t
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_get-format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
I have a very similar situation. I converted American date format "%Y-%d-%m" to correct format '%Y-%m-%d'. This is how I did it...
update table_name set my_date = DATE_FORMAT( STR_TO_DATE( my_date, '%Y-%d-%m' ) , '%Y-%m-%d' );
The first date string format '%Y-%d-%m' needs to be how the date is currently formatted in your table. The second date string is what you want to convert it to.

How to convert and use varchar data type as a datetime in mysql

Hello all,
This is the format of my my-sql data type "rdate".
Apr 1 2011 01:13:00:000PM
I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success.
I am trying to use date_format(str_to_date(rdate, '%m/%d/%Y'), '%Y%m');
Thanks
Mypixel
Try doing:
ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')
From the docs:
Your Date is in the Following format:
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
...
You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.
sqlfiddle demo
In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:
str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')
Just convert your column for good to datetime.

epoch to date on mysql

I've tried using
FROM_UNIXTIME(`date`)
and got a yyyy/mm/dd hour
how can I reverse it, that is have it as
hh:mm:secs mm/dd/yyyy
Thanks
Add specifiers to FROM_UNIXTIME or use DATE_FORMAT:
Specifiers
%T Time, 24-hour (hh:mm:ss)
%m Month, numeric (00..12)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
SELECT DATE_FORMAT(FROM_UNIXTIME(`date`), '%T %m/%d/%Y)
Or
SELECT FROM_UNIXTIME(`date`, '%T %m/%d/%Y')
Use DATE_FORMAT
DATE_FORMAT(FROM_UNIXTIME(`date`), "%T %m/%d/%Y")
This will return hh:mm:ss dd/mm/YYYY from unix time stored in database.
you have to define your custom date/time format. please have a look at:
http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_from-unixtime
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(), '%h:%i:%s %M/%D/%Y');