PHPExcel putting string date into .xls - php

guys. I have dates at DB as strings "d/m/Y".
When I put this dates into Excel, it shows me normal dates like 03/10/2000, but when I click on the cell, it show me the value '03/10/2000. So, the value is string, and not date as required for me.
So, I transformed the date from DB to a PHP date object and get sure with var_dump that date is Date object with that code :
$date = date_create_from_format('d/m/Y', $array[$i-20]['Date']);
$date->format('m/d/Y');
but anyway, the values in generated .xls are with ' before value.
Why it appears and how can I solve it ?

MS Excel stores dates as a serialized timestamp, the number of days since 1st January 1900 (or 1st January 1904 if using the Mac calendar). To store a value as a date (and not simply as a string), you need to convert your string date to a serialized timestamp, and then set the format mask for the cell.
$date = date_create_from_format('d/m/Y', $array[$i-20]['Date']);
$excelDate = PHPExcel_Shared_Date::PHPToExcel($date);
Set $excelDate as the cell value
$objPHPExcel->getActiveSheet()->setCellValue('C11', $excelDate );
and then apply the format mask to that cell
$objPHPExcel->getActiveSheet()
->getStyle('C11')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
The 02types.php example script in the `/Examples` folder demonstrates this
EDIT
You can (and it's more efficient to) apply a style to a range of cells: just specify the range in the getStyle() call.
$objPHPExcel->getActiveSheet()
->getStyle('C11:C200')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
To get details of available format: PHPExcel has a number of pre-defined formats built-in, and those can be found as constants in Classes/PHPExcel/Style/NumberFormat.php. However, you're not limited to that set of formats; and (in the same way that MS Excel lets you define "custom" formats) you can simply pass a string defining the format (e.g. "d-mmm-yyyy") using pretty much any of the rules that can be used for MS Excel number format masks as the format

Related

PhpSpreadsheet's formating the date importing from XLSX

Good Day. I use PhpSpreadsheet's library to import xlsx.
In The column I in xlsx I have this value 20/10/1970 00:00:00 displaying like 1970-10-20 00:00:00 but when I import it:
$dataArray = $spreadsheet->getActiveSheet()
->rangeToArray(
'A2:AO3', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
NULL, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
TRUE, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
TRUE // Should the array be indexed by cell row and cell column
);
I receive strange ["I"] => float(25861) Please Help me to get Carbon data in this field.
If I try to convert this float like a timestamp I receive 01/01/1970 not 10/20/1970
Other date I receive like ["J"] => float(43195.4092014)
In Excel dates are not the same as in other software/programming languages.
Excel counts from 1900-01-01 unlike UNIX that counts from 1970-01-01.
The number returned is a float value of the number of days from 1900-01-01, and the fraction of the float is the time of the day.
As an example 25861.5 is 1970-10-20 12:00 ("Y-m-d h.i").
To convert to UNIX you should take the returned value - 25569 (1970-01-01 in Excel) and multiply with 86400 (one day in seconds).
See here:
https://3v4l.org/AudMn
In Excel if you format 25861 and 25569 as dates:
The other float you got 43195.4092013889
echo date("Y-m-d h:i", (43195.4092013889-25569)*86400); //2018-04-05 11:49 (mind the timezones)
https://3v4l.org/mMZQ4
Another way to solve that feature is getting the active sheet from our object PHPSpreadSheet and change the style as follows:
$sheet = $PhpSpreadSheetObject->getActiveSheet();
$sheet->getStyle("A1:Z1")->getNumberFormat()->setFormatCode("YYYY-MM-DD");

How to read a date with ExcelBundle [Symfony Bundle]

I'm having an issue while I'm trying to read cell value with $cell->getCalculatedValue() of a DateTime cell.
The excel file value is 28/2/15 (in format d/m/y) but I get 42063.
Is this a problem with the format? If it is, how I can solve it?
MS Excel date/time values are a float, not a formatted string, or any other special datatype; and it's only the number formatting mask that differentiates them from any other float
getCalculatedValue() will handle any formulae, but won't apply any formatting, so you'll simply get the float value back
however
getFormattedValue() will calculate any formula for the cell, but also apply any number formatting mask, so a "date" will be returned as a formatted string dependent on the number format mask for that cell
EDIT
The formatted string returned is dependent on the number format mask used.... unfortunately some MS Excel date format masks are locale aware, but PHPExcel is not, so if the cell uses one of these masks then the result may not be formatted as you'd want.
You can apply manual formatting for a cell value, using a format mask of your choice:
$result = PHPExcel_Style_NumberFormat::toFormattedString(
$cell->getCalculatedValue(),
PHPExcel_Style_NumberFormat::FORMAT_DATE_DDMMYYYY
);
or
$result = PHPExcel_Style_NumberFormat::toFormattedString(
$cell->getCalculatedValue(),
'dd/mm/yy'
);
Alternatively, you can convert that MS Excel serialized timestamp to a unix timestamp using
$result = PHPExcel_Shared_Date::ExcelToPHP(
$cell->getCalculatedValue()
);
or to a PHP DateTime object
$result = PHPExcel_Shared_Date::ExcelToPHPObject(
$cell->getCalculatedValue()
);
and then use normal PHP date() or DateTime::format() masking to format it however you want
EDIT #2
To identify if a cell contains a date:
if (PHPExcel_Shared_Date::isDateTime($cell)) {
echo 'Cell ', $cell->getCoordinate(), ' contains a date value';
}

Formatting a date in PHPExcel that can be updated after XLS is generated

I'm try to output a date using PHPExcel where the format can be changed after generating the file.
So for example, I output the date, like this:
23/04/1989
And I can then format the cells, change the format and it shows something like:
April 23rd 1989
I've tried this approach (using the code below) and generated a file. When I click on the date cell and try to format it, it appears to have worked since it has the 'Date' category highlighted and the correct 'Type'.
However, if I change the format and click OK, the cell doesn't change it's format.
Here's my code:
$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', '23/04/1989');
$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
Is this a limitation in Excel or am I not going about this the correct way?
You need to convert your date to an Excel serialized date/time stamp, and store that in the cell
$dateValue = PHPExcel_Shared_Date::PHPToExcel( strtotime('23-Apr-1989') );
$objPHPExcel->getActiveSheet()
->setCellValue('A1', $dateValue);
$objPHPExcel->getActiveSheet()
->getStyle('A1')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
Note that PHPExcel_Shared_Date::PHPToExcel() accepts a unix timestamp or a DateTime object
$dateValue = PHPExcel_Shared_Date::PHPToExcel(
DateTime::createFromFormat('d/m/Y', '23/04/1989')
);
$objPHPExcel->getActiveSheet()
->setCellValue('A1', $dateValue);
$objPHPExcel->getActiveSheet()
->getStyle('A1')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
And as your date of 23/04/1989 would be treated as m/d/Y by PHP (and therefore not what you'd expected - see PHP's accepted format rules) using DateTime::createFromFormat() allows you to specify that it's d/m/Y

I need to import an excel file into database using PHPExcel the issue is with the date

I need to import an excel file into database using PHPExcel the issue is with the date,The date can be in any format in excel file.
when the date in excel file is in this format 12/12/2012 (m/d/y) it save correctly into database but if the format date in excel file is this 24/11/2014(d/m/y) it upload blank
when 24/12/2012(d/m/y) the value is 2036-03-01
$data = $objWorksheet->getCellByColumnAndRow($col, $row);
if(PHPExcel_Shared_Date::isDateTime($data)){
$cellValue = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$dateValue = PHPExcel_Shared_Date::ExcelToPHP($cellValue);
$dob = date('Y-m-d',$dateValue);
} else{
$datedata = "NULL";
}
hope you can help me guyz
The important thing here to be reminded of is that Excel stores dates and times as serial date or serial date-time. This is the number of days since Jan 0, 1900 plus a fraction of a 24 hour day. This article contains a good explanation.
The fact that you are getting a negative serial date like -454654643 probably mean that the formatt type of the cells in your Excel spreadsheet is incorrect.
There are two things you need to verify:
In Excel, use Ctrl + ` to reveal the serial date value of each date cell. None of these values should be negative. Then use Ctrl + ` to switch back to the default formatted-date view. (The ` key is the one above the tab key on your keyboard).
Right-click on the date cells, navigate to Format Cells > Number > Date > Type, and ensure that the date format specified in Type matches your intent. So if your dates are to be interpreted as d/m/y, make sure the Type matches. And likewise, for m/d/y.
Hope this helps.
I would try formatting the data from the SELECT statement you are using. Let the DB do the time work. Let the PHP script to the real work.
Here is a link to the MYSQL Reference guide that explains all the many way to play with date and time on the database side.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
What you are looking for is
SELECT date_format('2014-10-13 22:00:00, '%m-%d-%Y') from <table> WHERE <column> = <data>
this will output 10-13-2014
From here you should be able to figure out the rest.

Using PHPExcel to import contents of excel file into MySQL database (format problems)

I'm importing the contents of an Excel 2007 file into a MySQL database but the date field in the spreadsheet is a custom date format (dd-mmm-yy). I cannot change this to text because a client will be uploading it so it is not feasible. When I import the contents of the file it inserts the date as a number e.g. 40978 instead of 12-Jan-09. I know that changing the database table format of the field will have no effect as its excels formatitng but does anyone know a way around this? Without changing the format of the spreadsheet?
Use PHPExcel's built in conversion methods like PHPExcel_Shared_Date::ExcelToPHP() or PHPExcel_Shared_Date::ExcelToPHPObject() to convert the date values to a PHP/Unix timestamp or a PHP DateTime object respectively.
You can then format this to a yyyy-mm-dd format string using the appropriate PHP date() or $DateTimeObject->format() functions.
EDIT
$excelDateValue = 40978;
$PHPTimeStamp = PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
echo $PHPTimeStamp,'<br />';
echo date('Y-m-d',$PHPTimeStamp),'<br />';
or
$excelDateValue = 40978;
$PHPDateTimeObject = PHPExcel_Shared_Date::ExcelToPHPObject($excelDateValue);
echo $PHPDateTimeObject->format('Y-m-d'),'<br />';
Incidentally An Excel datestamp of 40978 is 10th March 2012 (based on the Excel Windows 1900 Calendar) or 11th March 2016 (based on the Excel Mac 1904 calendar). 12-Jan-09 would be an Excel timestamp of 39825 (based on the Excel Windows 1900 Calendar).
Use code thar will convert the string date into a mysql formatted date. Something like:
$ma=array("jan"=>"-01-","feb"=>"-02-","mar"=>"-03-", etc );
$month=$ma[strtolower(substr($exceldate,3,3))];
$newdate="20".substr($exceldate,7,2).$month.substr($exceldate,0,2);

Categories