I have script that daily download "test.csv" from client ftp, in that csv file I have one date field in the format ('mm/dd/yyyy') and table has field xd format ('yyyy-mm-dd').
I upload csv file using load data command.
Please suggest me how can i handle this situation.
If there is any way i can change date format with alter table command or load data have any option to convert to table format.
Any help will make my work easy.
Thank you.
In PHP you can write a script to convert your date to a time and then convert the time into a new date.
Something like this:
$time = strtotime( $originalDate );
$newDate = date( 'yyyy-mm-dd', $time );
See the php documentation for exact syntax
Date: http://php.net/manual/en/function.date.php
Time: http://php.net/manual/en/function.strtotime.php
You can load the values into a user variable before assigning to the date field with STR_TO_DATE.
LOAD DATA INFILE 'test.csv'
INTO TABLE test
(#date_value)
SET date_field = STR_TO_DATE(#date_value, '%d/%m/%Y')
A downside to this approach is that you must specify the columns you are importing to.
Related
This is a simple solution, but I cannot get it to work even after reading many similar posts on the topic.
I am able to define a php variable to hold the current date because I can echo it properly:
$_date_today = date('Y-d-m');
echo $_date_today;
2017-31-03
But when I try to use that php variable to insert the date value into mySQL table, the value inserted is 0000-00-00. My date_order column in the table is defined as DATE.
While mySQL always stores the date in the database using the same format and I don't believe it can be changed, you can format the date however you want when retrieving it using the many mySQL functions available... see the following link: mySQL date manimpulation functions
try to change the date format cause mysql format is Y-m-d and you are entering Y-d-m
try use this $_date_today = date('Y-m-d'); insted of $_date_today = date('Y-d-m');
I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y
As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.
I tried to insert the date using the following functions:
1) date('d-m-Y');
2) (new \DateTime())->format('Y-m-d');
I understand these two functions do pretty much the same thing but I was not sure what else I should try.
MySQL threw the following error for both dates:
Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1
I am guessing this should be an easy fix but I can't figure out what is wrong.
I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).
Any suggestion is welcome!
$newdate= date('Y-m-d', strtotime('10-09-2015'));
or if you want current time just use
$now = date('Y-m-d');
If your msg_date column's structure is DATETIME or TIMESTAMP, the date format should be:
YYYY-MM-DD HH:MM:SS
which can be formatted through PHP like this:
$date = date("Y-m-d H:i:s");
Or if you already have a date, and you want it to convert to that format, we can use strtotime():
$date = date("Y-m-d H:i:s", strtotime($date));
For more date format, check this link.
The MySQL error message indicated that you had the date format the wrong way around.
Year must go first, then month, then day, as in:
date('Y-m-d') // right
In your first example, you have
date('d-m-Y') // wrong
In one of your examples above, you have it right, but you say you got the same response, so I assume that was not what you actually tried.
Another thing to note is that a MySQL TIMESTAMP column stores both a date and time. It's valid to give MySQL just a date (MySQL will just leave the time at zero), but if you have no need to store a time, you may as well make the column DATE instead of TIMESTAMP.
If you want to display your dates as d-m-Y then by all means do so, but they need to be sent to MySQL as Y-m-d.
I'm creating a mySQL db that will contain as part of its data, timestamps. These timestamps will be imported from a CSV file in which they appear like this - 20110701083231
My question is which is the best datatype to store these as?
In my attempts so far, during the import process (which I am doing via PHPMyAdmin) the end of the timestamp appears to get stripped out and they become 2011070000000.
Oddly, when I return to the CSV file, the entries there also appeared to be stripped out in the same way...?
Thanks in advance for any light you shed and for all previous help given.
Have a try with this statement?
LOAD DATA INFILE 'yourFile.csv'
INTO TABLE yourTable
(#var1)
SET theColumnWhereItShouldBe = STR_TO_DATE(#var1, '%Y%m%d%H%i%s');
Oh, and the column should be of type datetime or timestamp.
Save them as in the datetime format. That means you will need to do some formatting of those values before saving them but that isn't difficult to do:
$date = DateTime::createFromFormat("YmdHis", "20110701083231");
$date = $date->format("Y-m-d H:i:s");
The input e.g 1327500671 is acquired during post method. I would like to save it into the database date column defined as "registerDate Date".
I couldn't find what date/time function can do this. I can fix the Date type in DB back into varchar, which makes things easier but I still would like to learn what type of format is most used to save datetimestamp into the database in more realife applications, as I am thinking they won't save it as 1/25/2012 21:49:00, for example.
May I suggest storing the timestamps in the database and using the PHP date() function to make them human readable as and when necessary:
$timeStamp = 1327500671 //a timestamp from the database
$registerDate = date("j-m-y H:i:s", $timeStamp)
I had used javascript calender in my form. User have to input a date using this calender. And date is stored in the database but what I need is that only day of a date must be saved.
How can I do that as I cannot make changes in javascript code as i m not good at it.
$date_customer=date("d",strtotime($_POST['datum1']));
I had also tried it by changing the column name to "tinyint" but didn't work :( .... it only stores 127 and shows 1 when record is viewed from database.
Instead of sending date to server you could send the day by using
.getDay() method of javascript Date object.
I dont know the format of your date you get in your text input (when you click on one of the days in your calendar) but i'd suspect it to be dd/mm/yyyy or mm/dd/yyyy
So your php will need to be the following to only get the day
$date = explode("/",$_POST['datum1']);
// if format is dd/mm/yyyy then use the below
$date_customer = $date[0];
// otherwise if format is mm/dd/yyyy then use the below
$date_customer = $date[1];
Check out the explode function
i would save the date in MYSQL as an INT by using this function (save it as a unix timestamp) which would be helpful in comparing dates later on (up to the second) or add/remove days/years/months .
the idea would be send the whole date string generated by javascript to the PHP script "dd/mm/yyyy" ,
then in php using the explode function and create the unix timestamp using the mktime function
then save it to the database as an int ,
then when you want to read it , use the php date function to know the day/month/year/hour/second/minute , you could then also add hours (+3600) or days (+3600*days) etc... , or even get range of dates and many other functionalities you may use later ...
cheers
I suppose that you use mysql (TINYINT are mysql specific).
TINYINT are integer and in php integer are usually not prefixed by zeros
Use a DATE field and format it when you report it.
You can use mysql date_format(date,"%d") function in your query.
Or the php date function.
select date_format(date_field,"%d") from some_table;
You can use a VARCHAR(2) to store the date (ugly).
If you stick to store only the DAY in an int or tynint.
use sprintf("%02d",$day) to format it correctly in php.