Trying to parse excel sheets but running into issues with user-defined date formats. The date format does not get added to the formattedData. I have no way of knowing what cell is going to contain a date and the dates come in all types of formats.
Example: M/D/YYYY - 11/17/2015
After parsing the sheet I get the floating point date with no format. I did not echo out the format from PHPExcel on this one.
In another case I echoed out the get format data and it was General for the column column that contained the date, but in excel it had a valid date format albeit user defined of D-MMM-YY - 15-Jul-14.
Testing for dates in these scenarios did not work either using,
PHPExcel_Shared_Date::isDateTime($cell);
So to the question, how do I get the date format or test if it is a date when the format is user defined?
Same issue as PHPExcel number formats for dates
It is using built-in number format id's for custom date formats.
Related
I am querying an API and I got a date like this: "2019-04-17T14:04:24.224-04:00". I suppose this is an iso date. How can I store the year, month and day on a mysql database? I want the following format "dd/mm/yyyy".
Formats are a display concern, that is it's a function of who is looking at the data, so you don't do it until you know who's looking at it. At the point of display you apply the format required by the user's locale, and quite often adjust for their local time-zone as well.
MySQL's DATE column type is expressed in ISO-8601 format like you have, so you should be able to insert that minus everything after the "T".
So either strip that out before insertion and it should be fine.
On display you can use any of the PHP date formatting functions to get the exact format you want. This is often locale specific.
You'll want to be sure that the time expressed is in the correct time-zone. Do any conversion necessary to get it in the right zone before converting to a date or you may find you're getting the wrong date for several hours of the day.
Generally you should:
Store dates as ISO-8601 formatted values in MySQL's native DATE or DATETIME fields and convert to/from localized formats as dictated by user preferences.
Store any time values as UTC and convert to/from local time as dictated by user preferences.
date("d-m-Y", strtotime("2019-04-17T14:04:24.224-04:00"));
Just do this,
Try this!
update TABLENAME
set date_columnname = LEFT(date_present_columnname,10)
I use PhpSpreadsheet to create XLSX files. The problem is that I set the date inside the cells with a formatCode but when I create a chart based on these cells the formatCode is not applied on them. This leads to having timestamps inside the chart and a proper date format inside the cells. Does anyone know how to pass the right date format into the chart?
Best regards,
laobiz
You may need to apply date formatting to the cell, e.g. :
$worksheet->getStyleByColumnAndRow($column, $row)
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME);
See supported date formats here.
Please note, that MS Excel does not work with unix timestamps like PHP (meaning the number of seconds since 01.01.1970). Instead, Excel counts
the number of days since 01.01.1900 and works with float values when converting to hours/minutes/secs. Therefore you cannot insert into a cell unix timestamp, but convert it into Excel timestamp before. See PHP conversion functions from and into Excel serial date format.
simple question here, I'm using PHPExcel to export a datagrid to an Excel file. In my datagrid, I have a few columns that contain dates and are formatted as 'Y-m-d'. When I extract the grid data, Excel does not recognize the data into those columns as Dates (Excel says that they have a General format and that they have no specific number format).
Is there anyway that I can format the dates so that Excel can recognize them as dates before the data is sent to Excel?
Thanks in advance
PHPExcel have in-built package to set number data as an excel date.
If you i got you right then you are just passing number value as an date and getting type error.
Try like: $date = PHPExcel_Style_NumberFormat::toFormattedString($data, "d-M-Y");
Or refer doc link for more details: http://www.cmsws.com/examples/applications/phpexcel/Documentation/API/PHPExcel_Style/PHPExcel_Style_NumberFormat.html#constFORMAT_DATE_DDMMYYYY
As described in the PHPExcel documentation and in the Examples such as 02types.php
You need to convert those "formatted" dates to an MS Excel serialized date/timestamp
$dateString = '2015-12-21';
$excelTimestamp = PHPExcel_Shared_Date::PHPToExcel($dateString);
and then set that $excelTimestamp as the cell value
$objPHPExcel->getActiveSheet()->setCellValue('C9', $excelTimestamp);
You should then set the format mask for the cell
$objPHPExcel->getActiveSheet()
->getStyle('C9')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
Is it possible to format a column (C2:C50) with PHPExcel with the following selected values:
I really can't get this to work..
Already tried something like:
$sheet->getStyle('C2:C50')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMMINUS);
But when I look a the cell properties it said the cell is formatted like:
PHPExcel only has a limited set of built-in constants for formats, but you can set the format code to any valid (custom) format just by using the appropriate string:
$sheet->getStyle('C2:C50')
->getNumberFormat()
->setFormatCode('d-mmm');
Note that locale-specific day/month names such as Woensdaag and Maart aren't supported by writers other than the MS Excel specific writers (ie. not in CSV, HTML, PDF)
Most custom format strings will work, though there are a few obscure ones such as $#,," M" that aren't directly supported, for display in formats other than MS Excel itself
I'm using PHPExcel to read an Excel 2007 document. In this document there's a column with "Time" formatting and it displays data correctly in Excel. When I try to parse this column I get some weird values. Any idea how can I fix this?
Thank you.
When parsing, you need to specify the format of the column. e.g.
$my_cell = $objWorksheet->getCellByColumnAndRow(1, 1);
$my_cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
print $my_cell_value;
Weird values?!? You mean you get a number instead of a human-readable date string.... it always helps to describe things accurately where possible, and weird isn't really an accurate description.
If you simply get the value from the cell, you'll be reading a raw timestamp value (MS Excel holds date and time values as a timestamp; like a Unix timestamp value in PHP, except that Excel's timestamp is the number of days since 1st January 1900 (or 1st January 1904, depending on which calendar it is configured to use).
MS Excel uses number format masking to display this timestamp as a human-readable date/time string.
You can use the getFormattedValue() method rather than simple getValue() to retrieve this as a formatted date/time (getFormattedValue() applies any number format mask for the cell to the value).
Not ethat if you've loaded the file with readDataOnly set to TRUE, then the number format masks aren't loaded, so PHPExcel cannot identify whetehr a cell contains a date or not.
Or, as James has suggested in his answer, you can convert the raw timestamp to a formatted value manually by applying number formatting with your own format mask
A third alternative is that you can use PHPExcxel's built-in date handling functions to convert this Excel timestamp value to a PHP/unix timestamp or to a PHP DateTime object (PHPExcel_Shared_Date::ExcelToPHP() and PHPExcel_Shared_Date::ExcelToPHPObject() respectively),
that you can then display using PHP's own date handling functions
The value for Time as it is stored in Excel is actually the fraction of the day, so 0.2 is 24 hours (or 1440 minutes or 86400 seconds) times 0.2. You can calculate the time of day based on that information, and then calculating from the beginning of the day. It makes it a little more usable than a formatted time, but a lot less readable.