Well, I don't know why it's happening but my following date field of excel displayed as numbers not even timestamp.
Screenshot of my excel csv file date field.
Now, when I read CSV date field will shows this date as following numbers in combobox using php and I don't know why it's displaying like that and what should I do to convert back to my csv file date.
Following date is displayed when I do data[$k] to do csv file reading:
41724 and 41731 and so on in combobox. What to do?
My code:
while($data = fgetcsv($handle, 1000, ",")) {
if($row == 10) {
break;
}
else {
if($mycount == 0) {
$mycount = 1;
$field_name = $data[$k];
$tot_combo = $tot_combo + 1;
} else {
**$user_data[]** = $data[$k]; //where date coming here
}
$row = $row+1;
}
}
So after did some more research I found in csv excel file my date column is assigned as date and when reading file in php it convert that date column to text which will return me 41708 not 10/03/2014 any idea how to achieve this Thanks
Because that number is an MS Excel serialized timestamp, it's how MS Excel stores date values and then uses a number format mask to render them as a human readable date.
A quick and dirty conversion to a PHP DateTime object:
function ExcelToPHPObject($excelDateTime = 0) {
$calendarBaseLine = '1899-12-30';
if ($excelDateTime < 60) {
// 29th February will be treated as 28th February
++$excelDateTime;
}
return (new \DateTime($calendarBaseLine, new \DateTimeZone('UTC')))
->modify('+' . floor($excelDateTime) . ' days')
->modify('+' . floor(fmod($excelDateTime, 1) * 86400) . ' seconds');
}
This function will return a PHP DateTime object (assumes MS Excel Windows 1900 calendar), and you can then use the DateTime object's format() method to format the date/time however you want, so:
$excelDateTime = 41724;
echo ExcelToPHPObject($excelDateTime)->format('Y-m-d H:i:s');
Related
I have structure table like: name varchar(15), gender smallint(1), birth_date date().
Controller:
public function actionImport()
{
$profile = new Profile();
$inputFile = 'files/profile.xlsx';
try{
$inputFileType = \PHPExcel_IOFactory::identify($inputFile);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFile);
}catch(Exception $e){
die('error');
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for($row = 1; $row <= $highestRow; $row++)
{
$rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row, Null, TRUE, FALSE);
if($row == 1){
continue;
}
$profile->name = $rowData[0][0];
$profile->gender = $rowData[0][1];
$profile->birth_date = $rowData[0][2];
$profile->save();
}
}
File: profile.xlsx
Data import success, but birth_date still 0000-00-00.
I try to print_r display like this:
Data saved in table like this:
A getValue() call on a field containing a date should return a value like 41959.... that is, an MS Excel serialized datetime stamp based on the number of days since 1st January 1900 (or 1st January 1904 if the file was created using the Mac version of MS Excel), and (by default) rangeToArray() uses a getValue() call.
To get a formatted date string, you need to call getFormattedValue() instead; and PHPExcel then uses the number format mask for that cell to format the date according to that mask. You can set rangeToArray() to do this for you by setting the $formatData argument to true.
$rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row, Null, TRUE, TRUE);
This will format all cells as they appear in the spreadsheet in Excel itself.
Alternatively, PHPExcel does provide functions to convert date values between Excel timestamps and unix timestamps, or PHP DateTime objects, and you could use those instead:
$profile->birth_date = PHPExcel_Shared_Date::ExcelToPHPObject($rowData[0][2])
->format('Y-m-d');
Dependence of your image, you try to save int number in date
field...so that the error is gone....
To resolve issue, you need to change number to date like this Y-m-d such as: 1991-11-20, this is MySQL date format, or you need to change MySQL field type to int if you need to save this number in database.
In PHP:
echo date('Y-m-d');
And you can convert date from unix by this:
$unixtime = 1307595105;
echo $time = date("Y-m-d", $unixtime);
for your example:
$profile->birth_date = date("Y-m-d", ($rowData[0][2] - 25569) * 86400);
OR
$profile->birth_date = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($rowData[0][2]));
I am working on staff attendance. User can upload an attendance sheet and my system will give the view of attendance excel sheet to user and then he can save it to database. The values from excel sheet saving successfully in database.
The problem is user can add record in columns of excel sheet in any format but when I get in PHP the date format goes wrong.
e.g.
when user add attendance date in column, the format of column is date and when he can save in out time it may be number format and when user upload this excel sheet, the date preview gone in unix time stamp.
Here is examples in images
Date in Excel sheet
Time in and out in excel sheet
When user go to site and upload this excel sheet
Then a popup window open
Then user browses the excel sheet and then I show the excel sheet view, that viewing wrong
You can see in above image that the date is being converted in unix time stamp and time in and out in numeric.
I want this view when a user can upload an excel sheet.
So hope you understand my issue and what I want.
Here is my upload and view excel sheet code:
$excel_file = $_FILES['file']['name'];
$target = SERVERPATH . 'assets/attendance_excel/';
$ext = pathinfo($excel_file, PATHINFO_EXTENSION);
$filename = rand() . date("YmdHis");
move_uploaded_file($_FILES['file']['tmp_name'], $target . $filename . '.' . $ext);
include(EXCEL_CLASS);
$inputFileName = $target . $filename . '.' . $ext;
// chmod($inputFileName,0777);
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
$objWorksheet = $objPHPExcel->getActiveSheet();
// $objWorksheet->getStyle('A1:E433')->getNumberFormat()->setFormatCode('Text');
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestDataRow();
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
$td_data = '';
$row_no = 0;
for ($row = 1; $row <= $highestRow; ++$row) {
$check_empty_column = 0;
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
if ($objWorksheet->getCellByColumnAndRow($col, $row)->getValue() == null) {
$check_empty_column++;
}
}
if ($check_empty_column < $highestColumnIndex) {
for ($col = 0; $col < $highestColumnIndex; ++$col) {
$td_data .= '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>';
}
}
$tr_data[$row_no] = $td_data;
$td_data = '';
$row_no++;
}
$data_all = array(
'data' => $tr_data,
't_col' => $highestColumnIndex,
'name' => $filename . '.' . $ext
);
echo json_encode($data_all);
I also search a lot about this and found something like this setFormatCode, PHPExcel_Style_NumberFormat::FORMAT_GENERAL etc but its not working for me. I try my best.
So please give me solution for this problem.
Note: I can not restrict a user that he convert the columns and than add data.
Hope you understand my question
MS Excel stores dates as a float value, a serialized timestamp, the number of days since 1st January 1900 (or 1st January 1904 if the file is using the Mac Calendar), and it is the number format mask for the cell that displays it as a formatted date.
PHPExcel provides a series of methods for converting between that MS Exce serialized timestamp and unix timestamps or PHP DateTime objects
$unixTimestamp = PHPExcel_Shared_Date::ExcelToPHP($excelTimestampValue);
will convert the MS Excel timestamp to a Unix timestamp
$phpDateTimeObject = PHPExcel_Shared_Date::ExcelToPHPObject($excelTimestampValue);
will convert the MS Excel timestamp to a PHP DateTime object
And you can then use PHP's standard date() function or the DateTime object's format() method to format that value however you like.
$excelTimestamp = PHPExcel_Shared_Date::ExcelToPHPObject($phpDateTimeObject);
$excelTimestamp = PHPExcel_Shared_Date::ExcelToPHPObject($unixTimestamp);
will convert either a unix timestamp or a PHPP DateTime object to an MS Excel serialized timestamp value
If you want to change the way that an MS Excel serialized timestamp is displayed in a spreadsheet, then you need to use an Excel format mask that corresponds too a date/time mask.... PHPExcel_Style_NumberFormat::FORMAT_GENERAL is not a format mask for dates/times
I am uploading an Excel file through PHP Excel. The column C has dates in format mm/dd/yyy.
When I echo the date for example '10/3/2016' it reads it as 42646. The format set for the column is short Date.
How can I read it as the normal date instead of 42646.
here is the model
function fi_upload($file_name){
$file = './uploads/'.$file_name;
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load($file);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
}
$data['header'] = $header;
$data['values'] = $arr_data;
$datecell = $objPHPExcel->getActiveSheet()->getCell('P2');
if(PHPExcel_Shared_Date::isDateTime($datecell)) {
$InvDate = date($format="Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($datecell));
}
echo $InvDate;
exit();
foreach ($arr_data as $q){
echo $q['C']; // column of date, date is echoed as 42646
exit(); //here i exit to display the date
}
I have to later insert it in the database as well in the yyyy/mm/dd format (which I can do if it is read properly)
Thanks.
UPDATE:
So apparently it was an Excel file issue, not php. Converting the cell values through TEXT() was the solution as suggested by #Hallur.
UPDATE 2.0:
I am trying to do it via PHPEXCEL method but I get the following two issues.
: Object of class PHPExcel_Cell could not be converted to int
Echoes date as 2036-02-07 where as the date in cell is 10/4/2016
42646 is an MS Excel serialized timestamp value, the number of days since 1st January 1900 (or 1st January 1904, depending on whether the spreadsheet is using the Windows or Mac calendar)
PHPExcel provides a variety of functions to convert values between MS Excel serialized timestamps and Unix timestamps or PHP DateTime objects (or vice versa), all of which can be found in the PHPExcel_Shared_Date class, e.g.
ExcelToPHP() to convert an MS Excel serialized timestamp to a unix timestamp
ExcelToPHPObject() to convert an MS Excel serialized timestamp to a PHP DateTime object
Alternatively, using the getFormattedValue() method instead of getValue() will return a formatted date/time string value instead of the serialized tiemstamp
$field1= $objWorksheet->getCellByColumnAndRow(0,$i)->getFormattedValue(); //Excel Column 3
$date = PHPExcel_Shared_Date::ExcelToPHP($field1); //unix
echo $field1= gmdate("Y-m-d", $date); //date
Already wrote answer this link
The answer to this question is here:
Excel weird behaviour with dates => text
The number 42646, is the amount of days since january 1st year 1900.
To convert the excel base date to unix time ( time() ), you must calculate the difference between unix start to excel base time start. (unix start: d-m-Y H:i:s -> 01-01-1970 01:00:00) (excel start: d-m-Y -> 1-1-1900)
First to get the difference, which is approximately 70 years and 1 hour (+ 1 extra day because excel date 1-1-1900 = 1, not 0.)
70 years, 1 day, 1 hour = 70 years + 25 hours. (to make up for leap years, we have to say 365.25 instead of 365 days in a year)
(70*365.25*24*60*60)+(25*60*60)
Now, we need to convert the excel date value to seconds as well: 42646*24*60*60 (~ 116 years)
We need to subtract them from each other to get the unix time, and since the excel date is higher than the 70 years, we put that first.
to echo out the day:
echo date("d-m-Y", (42646*24*60*60) - ((70*365.25*24*60*60)+(25*60*60)));
EDIT: I just realized that extra hour is because of my timezone.. CET, not UTC.
I am using phpexcel to import excel document to website. I have problem with getting Date format.
Date format is dd.mm.Y but after getting cellValue I get samo random numbers ( float(42391) )
This is my code:
$datum = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
this shoud get date?
$file_name = clean_uri($_FILES['excel']['name']);
$file_name = 'povijest_'.$file_name;
$folder_s = 'upload_data/excel';
move_uploaded_file($_FILES['excel']['tmp_name'], '../'.$folder_s.'/'.$file_name);
$objPHPExcel = PHPExcel_IOFactory::load('../'.$folder_s.'/'.$file_name);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$highestColumm = $objPHPExcel->getActiveSheet()->getHighestColumn();
$colNumber = PHPExcel_Cell::columnIndexFromString($highestColumm);
$arrayCount = count($allDataInSheet);
for($i=4;$i<=$arrayCount;$i++){
$datum = $objPHPExcel->getActiveSheet()->getCell('A'.$i)->getValue();
$prva = $objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
$zadnja = $objPHPExcel->getActiveSheet()->getCell('D'.$i)->getValue();
$najvisa = $objPHPExcel->getActiveSheet()->getCell('E'.$i)->getValue();
$najniza = $objPHPExcel->getActiveSheet()->getCell('F'.$i)->getValue();
$prosjecna = $objPHPExcel->getActiveSheet()->getCell('G'.$i)->getValue();
$promjena = $objPHPExcel->getActiveSheet()->getCell('H'.$i)->getValue();
$broj_transakcija = $objPHPExcel->getActiveSheet()->getCell('I'.$i)->getValue();
$kolicina = $objPHPExcel->getActiveSheet()->getCell('J'.$i)->getValue();
$promet = $objPHPExcel->getActiveSheet()->getCell('K'.$i)->getValue();
var_dump($datum);
exit;
It does get the date..... that isn't a random number, that's an MS Excel serialized datetime stamp. In the same way that you might use a unix timestamp in PHP, and then use the date() function to format that, MS Excel uses its own timestamp value, which is a count of the number of days since 1st January 1900 (or 1st January 1904 if created on a Mac). To display it formatted, you need to apply a number format mask.
The Cell's getValue() method will return the raw value from the cell, but getFormattedValue() will return the value with the number format mask applied
There are also documented methods (e.g. PHPExcel_Shared_Date::ExcelToPHP($msExcelSerializedTimestamp)
) for converting between the MS Excel timestamp and unix timestamp or DateTime objects, and for checking whether a cell contains a date or not
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.