lundi 18 octobre 2010

Getting the number of weeks in a year

function nbWeekInAYear ($year) {
        $nbWeek = date('W',mktime(0, 0, 0, 12, 31, $year));
        if ($nbWeek==1) {
            $nbWeek = 52;
        }
        return $nbWeek;
}

echo nbWeekInAYear(2009)."\n"; //53 
echo nbWeekInAYear(2010)."\n"; //52
echo nbWeekInAYear(2011)."\n"; //52
echo nbWeekInAYear(2012)."\n"; //52
echo nbWeekInAYear(2013)."\n"; //52
echo nbWeekInAYear(2014)."\n"; //52
echo nbWeekInAYear(2015)."\n"; //53
echo nbWeekInAYear(2016)."\n"; //52

dimanche 17 octobre 2010

Use of attribute in simpleXml is counter intuitive

Given this file


<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<reports>
<report id="1"/>
</reports>
XML;
?>


Accessing the id is attribute is done this way


$xml = new SimpleXMLElement($xmlstr);
$id = (string) $xml->report["id"];


If you forget the cast (string) you got an illegal offset warning, wich is due to the fact that attributes themselves are simpleXMLElement.

Finding the path of a config directory in symfony

I had to directly work with a config file in xml, finding the path need the use of constants thus in first place I build the paths with constatnts :

http://www.symfony-project.org/reference/1_4/en/03-Configuration-Files-Principles

$this->path = SF_ROOT_DIR . DIRECTORY_SEPARATOR .
"apps" . DIRECTORY_SEPARATOR .
"reporting" . DIRECTORY_SEPARATOR .
"config" . DIRECTORY_SEPARATOR
. "monitor.xml";

But I got this warning Notice:

Use of undefined constant SF_ROOT_DIR - assumed 'SF_ROOT_DIR'

You cannot directly use the constant you must use sfConfig::get

$this->path = sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR .
"apps" . DIRECTORY_SEPARATOR .
"reporting" . DIRECTORY_SEPARATOR .
"config" . DIRECTORY_SEPARATOR
. "monitor.xml";