Question

I am trying to calculate age based on birth date. Excel Formula is

=DATEDIF(K4,DATE(2014,3,31),"Y")=DATEDIF(K4,DATE(2014,3,31),"YM")

Which shows 9.7 if value in cell K is 2004-08-01 this is perfect but I am not able to set this formula in excel sheet using PHPExcel. Here is code what I am trying

$date = date('2014,03,31');
for($i=3;$i<=100;$i++){
$objPHPExcel->getActiveSheet()-setCellValue('AQ'.$i,'=DATEDIF(K'.$i.',DATE('.$date.'),"Y")'.'& . & '.'DATEDIF(K'.$i.',DATE('.$date.'),"YM")');
}

What is wrong with the code why excel file not opening after download and showing fileformat or file extension not valid.

Was it helpful?

Solution

Your Excel formula concatenation is with an invalid .. Assuming you want to display a . between the Year and the Months then you need to treat it as a string literal ("."):

$date = date('2014,03,31');
for($i=3;$i<=100;$i++){
    $objPHPExcel->getActiveSheet()-setCellValue(
        'AQ'.$i,
        '=DATEDIF(K'.$i.',DATE('.$date.'),"Y")'.'& "." & '.'DATEDIF(K'.$i.',DATE('.$date.'),"YM")'
    );
}

OTHER TIPS

not tested, try this,

$date = date('2014,03,31');
for($i=3;$i<=100;$i++){
$objPHPExcel->getActiveSheet()->setCellValue('AQ'.$i,'=DATEDIF(K'.$i.',DATE('.$date.'),"Y")'.'& . & '.'DATEDIF(K'.$i.',DATE('.$date.'),"YM")');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top