loadinfile issue while inserting date time into database - php

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?

Related

How to Import Excel to Database Using Laravel Excel

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

Timestamps are showing server time, not local time

I have a website where users(in different timezones) can create new records in a database, and i currently have each record being recorded with a timestamp of the time it was created... I am in EST time zone, but when i view the records that i have created on the site, they show the time in MST, which also happens to be the servers time zone... When i view the records in the database, the timestamp field is not converting to utc, it is instead converting to mst... I have noticed that when i insert a record from phpmyadmin, i use the datepicker and select 18:35:01, but when i look at the record after its been inserted, it displays 15:35:01!!! This is very frustrating, and i have been reading around that timestamp fields should automatically convert to utc, which mine is not doing... Another thing is, i am currently using a date function to format the timestamp, which is displaying in 00-00-00 00:00:00 format, and that was giving me errors for a while so i started using strtotime, which may be part of the problem... Im not sure... When records are inserted into the database i am using null for the value so it will automatically use current local time... But the thing is, different people from different timezones will be creating records than people who view them... So i need to acommodate for all of that... Any suggestions as to what i am doing wrong?
You can set the timezone used by your PHP script
date_default_timezone_set('America/Los_Angeles');
View the PHP Manual for more details

Importing 14 digit timestamp from CSV to MySQL and avoid stripping out data

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

How to insert excel data into database within a date range

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

change date format with alter table

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.

Categories