Search code examples
phpphpexcel

PHPExcel format dates occuring this week


PHPExcel conditional formatting has some helpful conditions and operators...

I applied a condition like so:

$aCondition = new PHPExcel_Style_Conditional();
$aCondition->setConditionType(PHPExcel_Style_Conditional::CONDITION_EXPRESSION)
           ->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_EQUAL)
           ->addCondition('AND(($B2<>$B3),$B2<>"")');

$aCondition->getStyle()->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
$aCondition->getStyle()->getNumberFormat()->setFormatCode($dateFormat);   

$conditionalStyles = $sheet->getStyle('B2')->getConditionalStyles();
array_push($conditionalStyles, $aCondition);    
$sheet->getStyle('$'.$letter.'2:$'.$letter.'10000')->setConditionalStyles($conditionalStyles); 

However, when I get to the excel document... it marks the cell yellow even though it should not be... but if I then go to the cell and press enter... the cell then loses its yellow... and then the conditional formatting works correctly...

I apply formatting for that cell's rows like so:

        $sheet->getStyle('M2:M9999')
        ->getNumberFormat()
        ->setFormatCode("dd/mm/yyyy");

Solution

  • I found this little doozy:

    PHPExcel_Shared_Date::PHPToExcel( strtotime( "03/25/2014" ) )
    

    As soon as you apply this, the issue goes away.. This is because the formatcode is just a mask... the underlying data has to be of a special Excel date type.