PHPExcel date not appearing - php

I am trying to format a PHP date() object into a format that Excel can read as a date rather than as a string.
$row[] = PHPExcel_Shared_Date::PHPToExcel(date('d-m-y', strtotime($worksheet_one_job['Job']['date_of_order'])));
$worksheet_one->fromArray($row, NULL, $columnID.$rowID);
The format of the field $worksheet_one_job['Job']['date_of_order'] in the database is DateTime (2016-06-28 00:00:00)
When I check this in Excel it prints nothing at all, any ideas how I can set the cell as a date format that Excel can read rather than as a string?

Related

How to make mysql accept date in a particular format from a migration page?

I have a bunch of excel sheets which I will be uploading to the database; with date in the format 05-Sep-2019. Is there a way to make mysql recognize it as a date rather than a string? I can use replace in excel to replace '-' with '/' if '-' doesn't work.
Thank you.
You can use strtotime function of php and format as you want
$date = '05-Sep-2019';
$newdate = date('Y-m-d',strtotime($date)); //2019-09-05
$newdate = date('Y/m/d',strtotime($date)); //2019/09/05
$newdate = date('d/m/Y',strtotime($date)); //05/09/2019
Or in Mysql
Kindly Note that MySQL stores date in YYYY-MM-DD format by default.
One way is to convert all dates into YYYY-MM-DD format so that it will be compatible with MYSQL.
We can format the date column in excel before exporting it to MYSQL by following the below steps:
Select the column which contains the date
Right click and select format cells
Choose date under category(on left-hand side)
Then choose custom under category
Under type insert the format --> YYYY-MM-DD
You will see all the dates in the columns get converted into the format --> YYYY-MM-DD
Then you can import the date into MYSQL safely.
Another way is to keep the column datatype as VARCHAR and use TO_DATE() function to parse the string data into DATE format.
you can convert it into Y-m-d format
$date = date('Y-m-d',strtotime($your_excel_date)) // 2019-08-29
$date = date('Y/m/d',strtotime($your_excel_date)) // 2019/08/29

PHPExcel putting string date into .xls

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

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';
}

PHPExcel setting date format without writing every single value

My current version of code for outputting to an Excel file is as follows:
I have an array of the column names and an array of all of the data:
$objPHPExcel->getActiveSheet()->setTitle($titleArr[$i]);
$objPHPExcel->getActiveSheet()->fromArray($headersArr[$i], NULL, 'A1');
$objPHPExcel->getActiveSheet()->fromArray($dataArr[$i], NULL, 'A2');
The only other thing I've been doing is to autosize the columns
$objPHPExcel->getActiveSheet()
->getColumnDimension($col)
->setAutoSize(true);
Is there any way to let the sheet be filled with data from the array and just tell excel that certain columns are actually in a specific format?
The following creates a custom field in Excel, rather than a date field. It sorts but I have to manually tell it to treat it like a number.
$objPHPExcel->getActiveSheet()->getStyle($col . '2:' . $col. '500')->getNumberFormat()->setFormatCode('yyyy-mm-dd hh:mm:ss');
Is there any way to get it to treat it like a date without having to write each cell manually with statements like:
$objPHPExcel->getActiveSheet()->SetCellValueByColumnAndRow(3, $currentrow, PHPExcel_Shared_Date::PHPToExcel($timestamp));
$objPHPExcel->getActiveSheet()->getStyleByColumnAndRow(3, $currentrow)->getNumberFormat()->setFormatCode('dd/mm/yyyy');
If a whole column is filled with only date values, you can use getStyle() instead of getStyleByColumnAndRow() to get the whole column. Like this:
$objPHPExcel->getActiveSheet()->getStyle('C:C')->getNumberFormat()->setFormatCode('dd/mm/yyyy');
A coordinate in the format A:A is converted to A1:A1048576. The same works for 1:1 which translates to A1:XFD1. See setSelectedCells() in Worksheet.php for the different conversions.
There isn't any such thing as a date field in Excel.... a date is nothing more than a number with a format mask.
But you could try using a cell binder: the cell binder is called whenever you set a value in a cell (unless you set the value explicit). The advanced value binder provided as an option with PHPExcel that recognises most date and time formats and will automatically convert and set a format mask accordingly.
I put after $objPHPExcel->getActiveSheet()->fromArray to set date time in columns as shown below:
while ($row = mysql_fetch_assoc($result)) {
$objPHPExcel->getActiveSheet()->fromArray($row, null, "A".$red);
// Again set row values
$dateValuerr = PHPExcel_Shared_Date::PHPToExcel(strtotime($row[NasDatumPrijemaPredmeti]));
$objPHPExcel->getActiveSheet()->setCellValue("D".$red."", $dateValuerr);
$dateValuerr = PHPExcel_Shared_Date::PHPToExcel(strtotime($row[NjihovDatumPrijemaPredmeti]));
$objPHPExcel->getActiveSheet()->setCellValue("E".$red."", $dateValuerr);
$objPHPExcel->getActiveSheet()->getStyle("A".$red.":AF".$red)->applyFromArray($table_style);
$objPHPExcel->getActiveSheet()->getStyle("A".$red.":AF".$red)->getFont()->setBold(true);
$red++;
};
and then set column
$objPHPExcel->getActiveSheet()->getStyle('D2:E1000')->getNumberFormat()->setFormatCode(exportdatum);
My date is : define('exportdatum', 'dd.mm.yyyy'); // Date
This is date in my MySql DB
MySql Date : 2015-05-01
But I convert in query to
DATE_FORMAT(predmeti.NasDatumPrijemaPredmeti, '%d.%m.%Y') AS NasDatumPrijemaPredmeti

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