my csv import is not formatting the dates - php

I cannot seem to get the csv file to format the dates to UTC time on import to mysql. Here is my query and if i take out the sql variables and the set statement it imports the information fine but i need the dates to be formatted in utc time.
$query = "
LOAD DATA LOCAL INFILE '".$file."' INTO TABLE instructions
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(route_name,stop_sequence_number,stop_location_id,stop_action,#stop_arrival_time,#stop_departure_time,time_zone);
SET stop_arrival_time = STR_TO_DATE(#stop_arrival_time, '%m/%e/%Y %r');
SET stop_departure_time = STR_TO_DATE(#stop_departure_time, '%m/%e/%Y %r');
";
Am I not using the STR_TO_DATE format correctly? i looked at the information on the format and it said that %m for numbered month, %e for single digit day, %y for four length year and the %r for AM - PM time.

You do not have to convert the string to a date in order to import the date. As you have noticed it will except the statement after you have removed the STR_TO_DATE part of the import statement. However, you will instead get a bunch of columns looking like 0000-00-00 because mysql does not understand dates in mm/dd/yyyy format, etc... Mysql only excepts dates in YYYY-MM-DD format.
You can change this in Excel before doing your import.
In order to change the date format in excel: right click on the top cell. Choose format cells from the drop down list. change the local to something like 'Afrikans'. Choose the format that looks like 2001-03-14. Use the top cell to fill down. Then save the document.
Just a quick note: Excel sometimes tries to do too much and will revert this column back to a the English(U.S) default time zone. So, if you plan on doing more editing make sure that the column has not reverted back.
Here is a link to more string literals on dev.mysql.
This stored procedure and post might help you as well: Error code 1292
Edit:
Alex, your code has 3 semi-colons in it. Have you tried your original code as shown by you without the first two but keeping the last? As such it would be consistent with this Answer.

Related

Convert from arbitrary dates to single date format

I've been asked to migrate data from one table to another. The old data includes a series of dates entered in a mishmash of whatever format the user felt like using at the time, the new format requires separate FROM date and TO date fields in MM/YYYY.
So some dates are a single date, such as DD.MM.YYYY or DD/MM/YY or DD YYYY or YYYY or again whatever. Some dates are both dates, such as DD.MM.YYYY-DD/YYYY or DD/MM/YY--DD/MM/YYYY. So just a mess. There are only a couple hundred rows but I don't feel like going through and changing them manually if I can avoid it.
Most of the Google results are for converting one format to another format, how can I convert from a mess of formats to one?
Using a single expression to encompass all combinations is hard so just do the patterns one by one:
UPDATE olddata,newdata
SET newdata.date=STR_TO_DATE(olddate.date, "%d.%m.%Y")
WHERE olddata.id=newdata.id
AND olddata.date REGEXP '[:digit:]{1,2}\.[:digit:]{1,2}.[:digit:]{4}'
AND newdata.date IS NULL
Do this a multiple times for each date format, and use the right expression in STR_TO_DATE. Experiment selecting with the right regulate expression before doing the update.
Eventually you'll have enough record to edit manually

Excel Date to String conversion excel

I have a date in Excel which is a date, and the format of it is as 2012/04/19.
I want to replace the / sign with - so it will have the format 2012-04-19. I want to replace it when I insert to MySQL, using PHP. What is the query?
From the MySQL documentation:
MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts.
What this means to you is that an Excel date whose text format is 2012/04/19 can already be inserted as is into a MySQL date column.
Internally, most databases store dates and timestamps in seconds since the epoch. So with regard to the particular format your date data has in the database, you only need to worry about that at the time you query. If you want to query out your dates in the format 2012-04-19, you could use DATE_FORMAT(), e.g.
SELECT DATE_FORMAT(your_date_col, '%Y-%m-%d')
FROM yourTable;
Actually, MySQL might give 2012-04-19 as the default date format in output, but DATE_FORMAT covers you should this not be the case.

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

MySQL DB with 3,000+ records that need to be converted

I have a database, that was an initial dump of an Excel file via phpmyadmin. So no formatting or other logic was applied to the table. So I am essentially left with a table of "varchar" style rows. Of which include things like dates and times.
Example:
start_time, end_time, start_date, end_date .. which look like 7:30 PM, 11:00PM, 9/9/12, 11/22/12 and then some rows that are fine just the way they are. Unfortunately I can't convert the whole table to a better format as someone based a lot of functionality around this lousy design of whats called a table. The only thing truely going for it, is the table has an auto incremented ID for each row that I can associate with something. So I figure as a means of Patching things up while we fix a lot of this functionality that strips things apart and all else I could make a bridge of sorts.
Make a table that can have proper types of rows for the data types, and then be able to use some mysql functionality like BETWEEN() for example on start_date to get a listing of a - z but a limited listing.
So I am trying to figure out is there a way I can dump data from one table to the next but in the process of this query have it convert it over to the types I want such as datetime, combining start_date and start_time into a datetime format and likewise with the end times/dates?
Or is this something I am going to have to pull out in a heap, loop over it with PHP and have it insert new rows into the new table?
I would do that with a PHP script to read the original data and then process it into the correct date formats, then insert to the new table.
So the scenario is, you have a time and date separated and you want to combine them, then format them to the DB acceptable date format. I would do something like this:
date_default_timezone_set('America/Los_Angeles');
// you have separate dates and times like this
$date = '9/9/12';
$time = '11:00 PM';
// combine them into one string
$str = $date . ' ' . $time;
$dt = DateTime::createFromFormat('n/j/y g:i A', $str);
$DBFormat = $dt->format('Y-m-d H:i:s');
echo $DBFormat;
// outputs DB date format -> 2012-09-09 23:00:00
One thing I noticed was your first time example was 7:30 PM and the next example was 11:00PM. In the latter example, there is no space before the PM. I don't know if that is a typo on your part or if your data does vary like that, if it does then I would add a check and insert the space.
For this, you can follow the steps:
Create a new table in the same DB which have all the fields in the source table(the table that going to copy).
Create a PHP file.
write a MYSQL query to select all the values in the source table.
In the while loop, write a query to insert these rows into the new table.
before that convert the value of start_time, end_time, start_date, end_date columns to the required format using "strtotime() and date()" functions in php.
insert the new values in the new table.

MySQL datetime into PHP

I have found a proper solution to my "problem" but even after reading mysql pages, I don't understand the logic behind it.
I currently store registration information in my system in a "datetime" formatted field in one of my tables (YYYY-MM-DD hh:mm:ss).
When I want to display the data on one of my php pages, simply posting the exact field data shows the format mentioned above.
I would THINK simply using date("Y-m-d",$row["DATE"]) where $row["DATE"] corresponds to the particular row value would return the desired format.
Instead I have to use:date("Y-m-d", strtotime($row["DATE"])).
Why is this? My $row["DATE"] field is not a string in the first place. Should I be able to simple rearrange the data stored in a datetime field? Wasn't that the purpose of rebuilding my entire tableset to accomodate datetime?
MySQL has a built in function called date_format which you can use to display the date how you want to.
SELECT DATE_FORMAT(date_field, '%Y-%m-%d') as date_field FROM table_name
The manual has the list of formats and the variables needed to display it that way. Using this method there will be no need to have PHP convert it etc. Plus it is less code on PHP side for something MySQL can handle easily.
EDIT
Sorry, just read you were looking for an explanation.
PHP's date function takes in a UNIX timestamp, which MySQL is not using. MySQL uses a real date format IE: YYYY-MM-DD HH:MM:SS, as you know, this is to be compliant for years later. The UNIX timestamp has a limited range from something like 1969 to 2037 that it is valid for, which makes it really useful for "timestamping" of items such as a chat box message or items they are not expected to be around post those dates, where as the MySQL DATETIME should not die out until the year changes to 5 digits or the world ends.
Read the WIKI on UNIX timestamp for more information on it.
MySQL does allow you to select dates in unix timestamp format, which allows them to be used more easily in PHP, exactly as you requested.
The previous answer seemed to ignore this point, or downplay it due to the range restriction on the unix timestamp, but if it's what you're looking for...
SELECT UNIX_TIMESTAMP(datefield) as u_datefield FROM table
will give you the date in timestamp format, which you can use as you suggested in PHP:
<?php
$showdate = date("Y-m-d",$row['u_datefield']);
?>
As the previous answer suggests, unix timestamps do have a limited range, so if you need dates prior to 1970 or after 2038 it may not be suitable, but for everyday use today it's great.
The main advantage of using timestamps over date strings is that timestamps can be added and subtracted, which is much harder with a date in string format.

Categories