0

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;
}
11
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Aug 17, 2016 at 15:56
  • And is my description at the very beginning not useful ? Commented Aug 17, 2016 at 15:57
  • "Questions seeking debugging help ("why isn't this code working?") must include [...] the shortest code necessary to reproduce it in the question itself. [...] Commented Aug 17, 2016 at 15:58
  • replace " +1 day" with " next monday" and " -2 day" with " last friday" maybe that helps Commented Aug 17, 2016 at 15:59
  • OK, done. I trimmed the code as much as I could. Thanks Commented Aug 17, 2016 at 15:59

2 Answers 2

1

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')                 
Sign up to request clarification or add additional context in comments.

Comments

1

change it to:

if ((date('m') == 8) && (date('d') == 17))                 
{
    $returnValue = $iconPath . "logo_halloween.png";
}

Why test this:

if (("08" == 08))                 
{
   echo "OK 08 == 08";
}

if ((08=="08"))                 
{
    echo "OK 08 == 08";
}

You will see no OK on screen. Both tests fail. Its all about TypeCasting!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.