D
D
Daniil Vshivtsev2016-02-27 16:09:47
PHP
Daniil Vshivtsev, 2016-02-27 16:09:47

How to write dates to .xlsx using PHPExcel?

Good afternoon. The code:

$my_date = date('d.m.Y', time()+60*20);
$objPHPExcel->getActiveSheet()->getStyle('D2')->getNumberFormat()->setFormatCode('DD.MM.YYYY');
$objPHPExcel->getActiveSheet()->SetCellValue('D1', $my_date);

In the document, the cell takes the desired format (DD.MM.YYYY), but the date is written with an apostrophe at the beginning ('), i.e. as a result, instead of 02/27/2016 in the cell '02/27/2016 I
already read that the apostrophe at the beginning is a forced conversion to the string, but I have no idea how to get rid of it.
I broke my brain, I sat for 3 hours, I just didn’t try it, but I didn’t understand it. I turn to more experienced comrades for help.
I sit on open office, I save in xlsx. What else to add so that the date is recorded without an apostrophe?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
OVK2015, 2016-02-27
@dahujika

$xls = PHPExcel_IOFactory::load("test.xlsx");
  $xls->setActiveSheetIndex(0);
  $sheet = $xls->getActiveSheet();
  
  for($counter = 0; $counter < 10; $counter++)
  {
    $currentCellDate = date('d.m.Y', strtotime('+'.($counter).' day'));
    echo $currentCellDate."\n";
    $xls->getActiveSheet()->setCellValueByColumnAndRow(0, $counter + 1, $currentCellDate);
  }

  $objWriter = PHPExcel_IOFactory::createWriter($xls, 'Excel2007');
  $objWriter->save("test.xlsx")

In Excel in column 'B' function:
=TEXT(DAYWEEK(A1;1),"DDDD")
Result:
905eb60c889643b69832c4494354a9e2.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question