I have this date format in a string column-
yyyymmdd hhmmss Etc/GMT (for example - 20211022 173124 Etc/GMT)
How can I convert it to this date format? yyyy-mm-ddThh:mm:ss (2021-11-22T17:31:24)
I can't use the original report for uploading data for spesific system. the only format should be "yyyy-mm-ddThh:mm:ss"
MySQL has 2 functions: STR_TO_DATE to parse string to date and DATE_FORMAT to output in your desired format.
For your case it can be something like this:
select date_format(str_to_date('20211022 173124 Etc/GMT', '%Y%m%d %H%i%S'), '%Y-%m-%dT%H:%i:%S');
I have a string like "2020-05-14 14:16:36.000" I want to convert this string to this form "2020-05-14" but I cannot find "14:16:36.000" 000 mean in this part.
I must find this part because I want to use str_to_date function in mysql.
Thank you for your helps
You can use the DATE_FORMAT() function for your purpose.
Syntax :
DATE_FORMAT(date, format)
Here, date is the date string to be formatted and format specifies the format in which you want the output to be.
The following command will do your job -
SELECT DATE_FORMAT('2020-05-14 14:16:36.000', '%D %M %Y');
Output : 14th May 2020
You can use the following command if you want it in the format - YYYY-MM-DD :
SELECT DATE_FORMAT('2020-05-14 14:16:36.000', '%Y-%m-%d');
Output : 2020-05-14
The DATE_FORMAT() will strip convert the given string into date format which you mention as the second parameter. You can refer more about dates - here
I have a dateformat but to display on front end I would like to display the data like so.
SELECT STR_TO_DATE('5,2013','%m,%Y');
The result I would like to generate is 'May 2013'.
Why are you storing dates as string values? Mysql has dedicated data types for date and time values: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
When using date, you can easily use DATE_FORMAT and set %m,%Y as formatting (second argument).
SELECT replace(date_format(str_to_date('5,2013','%m,%Y'),'%M-%Y'),'-',' ');
As to the format just read the docs: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format. You need an uppercase M for the month name. And use DATE_FORMAT to get from a date to a string.
SELECT DATE_FORMAT(DATE '2013-05-01', '%M %Y');
Assuming that you have 5,2013 stored in your database, you need to use DATE_FORMAT after parsing the string:
SELECT DATE_FORMAT(STR_TO_DATE('5,2013','%m,%Y'), '%b %Y')
Why? Because it seems you don't have a date type stored in the database. So you parse it to date using STR_TO_DATE. This give you the default date format. DATE_FORMAT let you apply other formattings on it.
All avaliable formatting values are documented here: https://www.w3schools.com/sql/func_mysql_str_to_date.asp
In general, I would recommend to think about storing date objects instead of custom date strings like 5,2013. This avoids those castings, because you could directly use DATE_FORMAT. It also has benefits on filtering/ordering and performance. Using date types, you could easily sort by it or filter e.g. everything in month X.
I don't say its not possible with custom strings like 5,2013 but would be more complex and could result in bad performance if the db grows.
You can use the functions:
str_to_date() to convert the string (after concatenating the prefix '1' to your string) to a valid date and then
date_format() to reformat the date:
SELECT date_format(str_to_date(concat('1,', ?),'%d,%m,%Y'), '%b %Y');
Replace ? with your string.
See the demo.
Result:
May 2013
In one of my Mysql database table the dates are stored in the format 31-Jan-05.
I'm trying to convert this format to 2005-01-31 before inserting them in other tables.
I've tried in this way str_to_date(exam_date, '%d%M%Y'), but i encounter the following error
Incorrect datetime value: '31-Jan-05' for function str_to_time
Can't i change the date format from 31-Jan-05 to 2005-01-31 using str_to_date?
Thanks in advance.
Yes. But you have two problems.
The second parameter is the current date format. (i.e. of the string)
You need to have the proper format (i.e. %b instead of %M).
Read the docs the for str_to_date().
str_to_date(exam_date, '%d-%b-%y')
Note: If you don't have a zero padded day, then you need to use %e instead of %d.
I would like to convert yyyy/mm/dd to dd/mm/yyyy format.So, I am using strtotime() function.but when I get zero date like(0000/00/00) from mysql. It will be showing wrong date like(01-01-1970).
I need exact string like 0000/00/00 to 00/00/0000. any other function are there?
Try MySQL's DATE_FORMAT() function
SELECT DATE_FORMAT(dateColumn,'%d/%m/%Y') AS dateColumn FROM table...
Try to use strftime
strftime("%d-%b-%Y", strtotime($value['your_date']));