I am saving date of birth in MySQLusing my form with
db->insert()
method, but it's not properly inserted in the database. How can I save the input field value in the proper date format in MySQL? (In MySQL the type of dob is also set to "DATE".)
MYSQL DATE - A date in YYYY-MM-DD format
so you need to convert via sql or php
PHP
$date= date('YYYY-MM-DD',strtotime($old_date));
$date= date('Y-m-d',strtotime($old_date));
if you have taken your dateofbirth field's datatype as date then you have to insert date in yyyy-mm-dd format.
Related
I have a datetime field that saves data as a string. But I want to save it as a date type.
Is there any date data type in Postgre sql that accepts this kind of date format?
February 1, 2016, 10:17 AM
You can use TIMESTAMP, DATE or TIME field to save your date, but date variables/ fields in postgres doesnt save format.
http://www.postgresql.org/docs/9.4/static/datatype-datetime.html
You have to use postgres function to_date(text, text) to convert from string to date
http://www.postgresql.org/docs/9.4/static/functions-formatting.html
You have TIMESTRAMP and TIMESTAMP WITH TIME ZONE.
You can have all the information here: http://www.postgresql.org/docs/9.4/static/datatype-datetime.html
I have issue with this date m/d/y for example 01/09/15 but when i insert that in my table of field name date and declared date i adde to me 2001/09/15 how can i save this format 01/09/15 in field database of my table
You can't change the format of mysql dates. You could save your date as a varchar but you can't use any manipulation.
You can save it as a mysql date or a time int, and then cast it to the format that you want, using date function.
You can read more about this in this post:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
I hope this will be useful.
I used this tag in html for accepting the date
$date = date('d-m-Y', strtotime($_POST["date"]));
I used this code for converting the date. The date is converted successfully if I simply echo it but date is not getting inserted in database. It gets posted as 00-00-0000 but if click it in database then proper date appears.The type 'date' is used in database to store the date.
Please tell me how to store it in database in that converted format.
You can't store it in the database in that format in a date field. The database stores dates in Y-m-d format (YYYY-MM-DD), when you read the date out again and display it, then convert it to the format you want it to show as.
Make sure your table has a date with a date or datetime datatype.
Make sure that you store it in the 'Y-m-d' format.
The you could select it from the table:
SELECT DATE_FORMAT(my_date, '%Y-%m-%d') AS my_date FROM table;
Then echo the "my_date" entry; it should come out right.
If you wanted to select it in the d-m-Y format,
SELECT DATE_FORMAT(my_date, '%d-%m-%Y') AS my_date FROM table;
Thanks for help but I found the solution. We can simply enter the date by accepting input type='text' through HTML and store it directly in database and we can use this code while displaying data from database :-
$date = date('d-m-Y', strtotime($_POST["date"]));
Now we can see data in dd-mm-yy format now this works perfectly...
Hope somebody can advise on the following:
I have created a table into my database which has a coloumn with datatype defined as DATE. The normal MySQL date order is YYYY-MM-DD, however I need to enter it in format DD-MM-YYYY.
I am using php to insert data into my table, and I was wondering at what point should I convert the date format.
To summerize, I am asking the following:
Is it possible to insert into MySQL database where field is formatted as DATE, a date in format DD-MM-YYYY instead in format YYYY-MM-DD?
If not possible, can I convert a value posted from PHP in format DD-MM-YYYY into the format YYYY-MM-DD using the MySQL syntax or I should use PHP to post the value in YYYY-MM-DD format?
Thank you in advance
You would use STR_TO_DATE in your INSERT to convert the date to the ANSI standard date format.
INSERT INTO `table` (`date`) VALUES (STR_TO_DATE('01-05-2013','%d-%m-%Y'));
You can then use DATE_FORMAT to convert the ANSI standard date to the format you want.
A date is always stored in the same manner in the DB.
When inserting you have to give the DB a format that it can parse as date. And when reading from the DB you can format the date in any way you like.
So you have to format your data before inserting into the correct format. Otherwise the DB can't store it as date.
You can go to different mysql website and get this answer. For Example:
http://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm
SELECT STR_TO_DATE('21,5,2013','%d,%m,%Y');
I am trying to insert a date into an SQL table using a php script. I am generating the date in php using the following code:
Date("l F d, Y");
If I set the format of the 'dat' column format in my SQL table to string the date is entered correctly however if I set the column format to date the date appears as
0000-00-00
Any help?
The DATE and date-related data types store date values in an internal structure. The formatting of the date value you entered is not preserved. When saving to a string data type, it's just a string. No date knowledge is saved.
When you SELECT the data from a DATE column, the default format for display is 0000-00-00. You can format the value (technique depends on the DBMS) when you SELECT the data.