mysql database date field datatype - php

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');

Related

Postgresql date format with php

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

How i did not added 20 before date in mysql

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.

Convert date string to insert in mysql date format

I have a bunch of txt files that contains a lot of data, including a date in format
dd-mm-YYYY
for example this date
15-03-2014
Researching around here i found a way to convert this date to insert in database in the correct date format YYY-mm-dd
the query is
STR_TO_DATE('$array[12]', '%Y-%m-%d')
But i'm having weird results like
2019-07-20
How can i correctly insert them to database any tips of what i'm doing wrong?
The second parameter should be in the format your date is stores, not the format you're looking for which is always YYYY-MM-DD. So:
STR_TO_DATE('$array[12]', '%Y-%m-%d')
should be:
STR_TO_DATE('$array[12]', '%d-%m-%Y')

what is the format with which mysql stores date and time?

While creating a table, I defined one column of DATE type and one of TIME type. As I insert the values using a php script like :
date--> 2013-11-11
time--> 12:12:12
and when I query the sql browser I see those values in exactly the same manner. But I am unaware of the format with which it stores the date and time. Like yyyy-mm-dd or yyyy-dd-mm.
Is there any way I change it ?
Dates and times are stored in MySQL in the format "YYYY-MM-DD" and "YYYY-MM-DD HH:MM:SS" which is not necessarily the format you want to display in your web page or application. There are two methods to reformat the date and time into the desired format. One is to do it in the SQL query in MySQL calling the DATE_FORMAT() function and the other is to do it with the programming language retrieving the data from the MySQL database.
From MySQL 5.1:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
For second question: you can't change default DATE format for the storage, please see this question also
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format
http://dev.mysql.com/doc/refman/5.5/en/time.html
MySQL retrieves and displays TIME values in 'HH:MM:SS' format
I do not believe this can be changed. But you do not care. You can extract dates and times in the format of your liking with the DATE_FORMAT() and the TIME_FORMAT() functions.
If you want to know the internal storage of Date columns, you can check Date and Time Data Type Representation, but I think you want to select date in different format; which other guys already answered about it.
It is stored in 3 bytes, and it is always YYYY-MM-DD.
The datetime is in Y-m-d H:i:s format, or year month day and hour minute second. If you only use a part, the format stays the same.
If you want to change the format there are many ways. The easiest would be to do something like return date("Y-d-m H:i:s", strtotime($mysqldatetime)); (will turn it to dutch date);
Keep in mind that you are using two seperate columns, one for time and one for the date. If you use only one column the missing values are filled with default values (time would be 00:00:00 and date would be 1970-01-01

How to save data in a different format in MySQL database

I notice that MySQL save date as 0000-00-00 format. But in my application I have validate date for this 00-00-0000 format. I notice that date is not saving properly as different formats. Can I change mysql data format to 00-00-0000 format?
The 00-00-0000 format is not entirely clear; it could be dd-mm-yyyy for instance.
You can simply convert the date you have like so:
$mydate = '24-12-2012';
// create date object by using a known format
// see manual page for all format specifiers
$d = DateTime::createFromFormat('d-m-Y', $mydate);
// format date object in yyyy-mm-dd
echo $d->format('Y-m-d');
See also: DateTime::createFromFormat() DateTime::format()
You can also use MySQL str_to_date function
INSERT INTO yourtable (datefield) VALUES (str_to_date('01-02-2007', '%d-%m-%Y'));
As #Jack explained it is one of the correct way you can choose and
at the time of select date from database you can use
DATE_FORMAT(date,format).
You can't.
Instead of that you have to convert your custom format to database format and back.
To store it in the database you can use explode PHP function.
To when selecting, you can format mysql date format using DATE_FORMAT mysql function.

Categories