Laurent Laurent - 3 months ago 6
PHP Question

Multiple if structure in a php function

I am trying to run a php script which would return the name of a specific icon, depending on the day.

I have added this special method which computes the Easter day (Sunday).

Now, I have added a special case for today (17.08), just to test my algorithm. However, my $returnValue does not return the icon that I would like (i.e. assets/icons/logo_halloween.png) ... I know it's not halloween yet, but come on ;-)

I am probably doing something wrong here with my structure of the if's. Would be glad if you could assist me on this. Thanks a lot in advance.

function getIconFileName()
{
$iconPath = "assets/icons/";
$returnValue = ".";


// gets current year and stores it in a variable
$year = date('Y');

// Calcul des dates variables (Pâques)

// gets the Easter Sunday
$date_Easter_Sunday = easter_date($year);

if ((date('m') == (date('m', $date_Easter_Sunday))) && (date('d') == (date('d', $date_Easter_Sunday))))
{
// Dimanche de Pâques
$returnValue = $iconPath . "logo.png";
}

elseif ((date('m') == 08) && (date('d') == 17))
// ---> It looks like my code never returns this value (logo_halloween.png) <----
{
$returnValue = $iconPath . "logo_halloween.png";
}

// Calcul des dates fixes

elseif ((date('m') == 01) && (date('d') == 01))
{
// Premier jour de l'an
$returnValue = $iconPath . "logo.png";
}

elseif ((date('m') == 03) && (date('d') == 21))
{
// Premier jour du printemps
$returnValue = $iconPath . "logo.png";
}

else
{
// Ceci est un jour normal
$returnValue = $iconPath . "logo.png";
}

echo "Path to icon : " . $returnValue . "<br>";

return $returnValue;
}

Answer

Your problem is here

elseif ((date('m') == 08) && (date('d') == 17))                 

Change to

elseif ((date('m') == 8) && (date('d') == 17))                 

And it will work. When compare date('m') which return the string '08' with an integer, 08, php convert the string '08' to integer 8, and the result of 8 == 08 expression is false.

Or better:

elseif (date('m/d') == '08/17')                 
Comments