I'm using the spreadsheet-reader to read some .xls and .xlsx and convert them to .csv.
This is my simplified code:
require_once(dirname(__FILE__) . '/../ext/spreadsheet-reader/SpreadsheetReader.php');
require_once(dirname(__FILE__) . '/../ext/spreadsheet-reader/php-excel-reader/excel_reader2.php');
$pCsvPath = Config::$generatedCSVPath . "/" . str_replace(array(".xlsx", ".xls"), ".csv", $pFileName);
$csv = fopen($pCsvPath, "w");
$pReader = new SpreadsheetReader($pFilePath);
// Read only the first sheet
$pSheets = $pReader -> Sheets();
$pReader -> ChangeSheet(0);
foreach ($pReader as $row)
{
fputcsv($csv, $row);
}
Everything's ok, but I'm having problems with the dates.
There's a column on the .xls / .xlsx files with the following format 2015-02-10
When I write them to .csv, the output of this column is 2015201520152015-FebFeb-TueTue.
How can I convert it to .csv using the code I post above but maintaining the date as the correct format (2015-02-10)?
Thanks in advance.
EDIT: If I manually change the dates from 2015-01-01 to 01-01-2015 (put the year at the end) the dates are parsed correctly. If there's no way to save the date as 2015-01-01, how can I change that row to the correct format (year at the end) using the code I post above?
I know I can parse a date manually, but not using the code above, as everything gets loaded when I call the $pReader = new SpreadsheetReader($pFilePath) function and there's no way to manipulate only the date.
use date , strtotime
date( "d-m-Y", strtotime( "2015-02-10" ) )
will going to convert your date format to 10-02-2015
foreach ($pReader as $row)
{
// assuming date is stored on 3rd place , $row[2]
$str = $row[0] ."+_+". $row[1]."+_+".date( "d-m-Y", strtotime( $row[2] ) );
fputcsv($csv,explode("+_+",$str));
}
Related
I have a large excel sheet which I am inserting data by bulk upload.
My problem is date:
date format is like 09/11/2017. I need to insert this data into table.
Any one can suggest me how to do this by conveting in mysql date format?
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$dateFromExcel = strtotime('09/11/2017');
$newDateToInsert = date('Y-m-d',$dateFromExcel);
echo $newDateToInsert;
// 2017-11-09
if you are using PHP 5.2 or higher
use
$ymd = DateTime::createFromFormat('d/m/Y', '09/11/2017')->format('Y-m-d');
you need to get row first and then use like this.
This is a function to convert date from DD-MM-YYYY to YYYY-MM-DD :
function con2mysql($date) {
$date = explode("-",$date);
if ($date[0]<=9) { $date[0]="0".$date[0]; }
if ($date[1]<=9) { $date[1]="0".$date[1]; }
$date = array($date[2], $date[1], $date[0]);
return $n_date=implode("-", $date);
}
If you are using phpExcel library to uplod exccel file than you can use following line.
It works perfectly for me.
PHPExcel_Style_NumberFormat::toFormattedString($exceldate, 'YYYY-MM-DD');
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I'm trying to upload a csv file to my mysql table with php.
I heard about the inline command but the problem is that the csv date format is dd/mm/yyyy and the mysql format is yyyy-mm-dd.
I tried almost every Date convert function but it doesn't worked out. always see 0000-00-00 on the Mysql Table.
How can I convert it correctly?
This is the last thing I tried:
($date[0] is the date cell in the csv. - 26/05/2016, and the second column in the mysql is the date)
while ($data = fgetcsv($handle, 1000, ","))
{
$date = strtotime($data[0]);
$newDate = date("d-m-Y", strtotime($data[0]));
$trydate = date("Y-m-d", strtotime($newDate));
echo $newDate . "<br>". $date. "<br>" . $data[0] . "<br>". $trydate. "<br>";
$import="INSERT INTO `testresult` (cityid, date, testnum,result) VALUES('$data[1]','$newDate','$data[3]','$data[2]')";
mysqli_query($con,$import) or die(mysql_error());
}
The output was:
01-01-1970
(blank)
26/05/2015
1970-01-01
strtotime waiting an input an expected format.
You need to split your string, and create a Y-m-d fromat from it, or you can use the DateTime object.
$dateString = '11/05/2016';
$Date = DateTime::createFromFormat('d/m/Y', $dateString, new DateTimeZone(('UTC')));
echo $Date->format('Y-m-d');
OUTPUT
2016-05-11
With strtotime
$string = '26/05/2016';
$trydate = date("Y-m-d", strtotime(substr($string, -4) . "-" . substr($string, 3, 2) . "-" . substr($string, 0, 2)));
echo $trydate;
Result are the same.
I believe you need to convert the / to - so that strtotime understands the format..
for example
while(...) {
$date = date('Y-m-d', strtotime(str_replace('/', '-', $data[0])));
echo $date;
$import = "...";
}
You need to use the SET clause, along with a variable to reference the contents of the row at that column. In your column list, you assign your date column to a variable name. You can then use it in your SET statement.
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE testresult
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(cityid, #var1, testnum,result)
set dateOfBirth = STR_TO_DATE(#var1, '%d/%m/%y')
I have a problem trying to upload an excel to my database through PHP.
The format I have in the excel is dd/mm/yyyy and my sql needs yyyy/mm/dd
$_DATOS_EXCEL[$i]['ship_day']= $objPHPExcel->getActiveSheet()- >getCell($headings['Ship Date'].$i)->getFormattedValue();
I tried
getFormattedValue('YYYY/MM/DD');
but doesnt work
I really appreciate if you can help me out with this.
If your cell value really is an Excel serialized date/time value, and you know which cells contain these dates, then you can convert it directly to a PHP DateTime object
$dto = PHPExcel_Shared_Date::ExcelToPHPObject(
$objPHPExcel->getActiveSheet()
->getCell($headings['Ship Date'].$i)
->getValue()
);
$_DATOS_EXCEL[$i]['ship_day'] = $dto->format('Y/m/d');
or to a Unix Timestamp
$unixTimestamp = PHPExcel_Shared_Date::ExcelToPHP(
$objPHPExcel->getActiveSheet()
->getCell($headings['Ship Date'].$i)
->getValue()
);
$_DATOS_EXCEL[$i]['ship_day'] = date('Y/m/d', $unixTimestamp);
Add three line after
"$_DATOS_EXCEL[$i]['ship_day']= $objPHPExcel->getActiveSheet()- >getCell($headings['Ship Date'].$i)->getFormattedValue();"
this line......
$rearrange=$_DATOS_EXCEL[$i]['ship_day'];
$explode = explode("/",$rearrange);
$_DATOS_EXCEL[$i]['ship_day']= $explode[2]."/".$explode[1]."/".$explode[0];
Hi~ I am using PHPExcel 1.7.7 to read XLSX files and present the results in web pages.
I'm trying to get the value of a cell that stores a date & time string and process it in PHP using the following code.
<?php
date_default_timezone_set('Australia/Canberra');
$value = $objPHPExcel->getActiveSheet()->getCell('A1')->getValue();
define('MIN_DATES_DIFF', 25569);
define('SEC_IN_DAY', 86400);
function excel2Timestamp($excelDate)
{
if($excelDate <= MIN_DATES_DIFF)
{
return 0;
}
return ($excelDate - MIN_DATES_DIFF) * SEC_IN_DAY;
}
echo $result = date('H:i:s d/m/Y', excel2Timestamp($value));
?>
while the original value in the cell is "00:00:00 01/07/2012", instead of giving me the correct results, the output from the above PHP code shows "10:00:00 01/07/2012".
It seems that it is caused by the time zone differences, but I have already defined the time zone at the beginning of the code. No idea what is wrong with it.
Can anyone please help me with it? Thanks in advance.
Forget your function to convert to timestamp (I was doing the same too)... PHPExcel does the convertion automatically, so try doing this:
//get the excel date value (if it's stored in the A1 cell)
$excelDate = $objPHPExcel->getActiveSheet()->getCell('A1')->getValue();
//converts from excel format to a datetime object
$date = PHPExcel_Shared_Date::ExcelToPHPObject($excelDate);
//format the date as you want.
$formatedDate = $date->format('m/d/Y');
It should work.
I am fetching data from a database and outputting it as an XML file
I am using following format to fetch it
using for loop
$arrayName['fieldName'] which in my case is $row[publication_date]
In the database its in the format of mm/dd/yyyy ..but i just want yyyy to be outputted and I am trying the following code ::
$k = "SELECT EXTRACT(YEAR FROM'$row[publication_date]')";
if($k!=NULL){
$xml_output .="\t\t\t\t<year>" . $k . "</year>\n";
The result is
<year>SELECT EXTRACT(YEAR FROM'1/1/1995')</year>
But instead i want it to be just
<year>1995</year>
What i am doing wrong here?? plzz help me out..
Any questions plzz comment..:)
You can't just run SQL without executing a query in a database: you need to use PHP's functions.
$timestamp = strtotime($row['publication_date']); // convert date string to unix timestamp
$year = date('Y', $timestamp); // extract only the year
Store $year in your XML.
Reference:
http://php.net/strtotime
http://php.net/date
I would just pull the date normally without trying to format it on the database server and format it using PHP.
$date = // code here to gather the date
$date = date('Y', strtotime($date));
echo $date; // should return the 4 digit year
http://us.php.net/manual/en/function.date.php
Well, there's a few potential issues, but the first is that you need to actually use your database connection to run the EXTRACT command.
Alternately, with less overhead, you could use a string slice or regex to pull out the date.
How about:
$xml_output .="\t\t\t\t<year>" . preg_replace('#.*/#', '', $row[publication_date]) . "</year>\n";