I need to import attendance reports to my system using Laravel Excel Library, But I have some problem with the wrong data validation. For example, I have the following excel data that I want to import into my database.
As you can see John has put his finger three times into the machine, But I want to Insert only the first and last record into my database using Laravel excel library. And also the struct of my database is like the following picture.
As you can see In my DB structure I have time_in and time_out in the same row, but in the excel file time _in is in one row, and time_out is in another row, So how I can insert this excel file correctly in my Database.
You need to parse the data before sending it to the Database (DB), not just directly feed it to your DB.
A simple explode with ' ' as the separator can split the Date/Time column then use DateTime to format the date and time, meanwhile keep tracking which of the data you need as Time In and Time Out
Assuming:
$date = explode(' ',$dateTime);
$date[0] = the date, 2/1/2021
$date[1] = the time, 7:31
Assuming the date is j/n/Y,
attendance_date can be filled as $attDate = date_format(DateTime::createFromFormat('j/n/Y', trim($date[0]),'Y-m-d');
may be you have to parse data before store it into database
or your can change the Excel file contents
Related
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.
I am facing a problem, i need to upload a csv file with few columns in it including date time column into database. while uploading the data by reading each row and inserting into database it is working fine with date and time. but when i use load infile the whole date and time appears in database column of the table as 0000-00-00 00:00:00 the data type is date. and i am writing the data & time in csv file in the same format $date = date("Y-m-d h:i:s"); using php. what might be the reason?
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.
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");
I would like to insert some excel data in range of 21 days (from day 1 until day 21)into database. Use in PHP coding :)
Can anyone give me a helping hand ,
1) If it is the first time insert excel data into the database table, the data are the whole date in excel column, after that the database will check if table is NOT empty, it will just update the data.(Will this be possible?)
2)Lets for example : $today is 06/24 , the first day in excel file, it will only insert the whole data into database from excel file although the rest of data inside the excel column are empty. While if 06/24 is the 10th day in excel file, the database will only update data on 06/24.
thanks~
You can do this using php ExcelReader. It return all data in array after that you can insert it according to date rage (If exists then update otherwise insert) in your database easily. Find below link to download ExcelReader.
http://code.google.com/p/php-excel-reader/downloads/list