Ocean_Voyager Posted yesterday at 03:12 PM Share Posted yesterday at 03:12 PM Hi all, I hope you can help. I am trying to write php code so that a different image is displayed in my site header depending on the date. All year round is one image, but at Christmas a festive version is shown instead. Here is my code; <?php // Normal (6 Jan - 31 Nov) if ((date('m') == 06) && (date('d') >= 01) || (date('m') == 11) && (date('d') <= 31)) { echo "<img src="image/site/site-header.png">; } // Christmas (1 Dec-5 Jan) else if ((date('m') == 12) && (date('d') == 01)) || (date('m') == 01) && (date('d') <= 05)) { echo "<img src="image/site/site-header-christmas.png">; } ?> However, I am getting this error message; Parse error: syntax error, unexpected 'image' (T_STRING), expecting ';' or ',' in /homepages/29/d1007800584/htdocs/page/site-header-test.php on line 4 Sorry, I am very much a beginner so please forgive my ignorance / stupidity - what am I doing wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/326828-using-php-to-display-different-images-based-on-the-date/ Share on other sites More sharing options...
mac_gyver Posted yesterday at 05:20 PM Share Posted yesterday at 05:20 PM the syntax error(s) are due to missing quotes on the end of each string and reusing the same type of quote inside the strings. you are building and echoing literal strings. the initial and final quotes on each string must be the same type and be a matching pair. you are missing the final quote on each string. the > at the end are part the <img ... > tag and belong inside the string, e.g. echo "<img ...>";. any quotes that are within a string must either be the 'opposite' type, single vs double, or they must be escaped so that they don't match the initial quote, which terminates the string. the initial and final quotes around the src="..." attribute should either be changed to single-quotes or escaped with \ characters. i prefer less typing, so i would use single-quotes. next, your conditional logic only needs - if( normal case){ echo normal output } else { echo special output }. lastly, your conditional comparisons need some help. you need to test if the 'm' and 'd' part of the date is greater than or equal a lower value AND less then or equal to a higher value. also, for testing purposes, you need to be able to manually set the value being tested, so this value should be built in a variable, which will be a string, e.g. '0106' for jan 6th. as long as the fields making up a string are the same width, and from left to right, most significant to least significant, you can directly compare strings, e.g. if('0106' <= $some_variable ... i'll let you cogitate on how to create and test the conditional logic needed. Quote Link to comment https://forums.phpfreaks.com/topic/326828-using-php-to-display-different-images-based-on-the-date/#findComment-1650177 Share on other sites More sharing options...
Ocean_Voyager Posted 23 hours ago Author Share Posted 23 hours ago Thank you! I'll do some more work on it. Quote Link to comment https://forums.phpfreaks.com/topic/326828-using-php-to-display-different-images-based-on-the-date/#findComment-1650181 Share on other sites More sharing options...
Phi11W Posted 9 hours ago Share Posted 9 hours ago 16 hours ago, Ocean_Voyager said: if ((date('m') == 06) && (date('d') >= 01) || (date('m') == 11) && (date('d') <= 31)) Just a word of warning - get out of the habit of prefixing numeric values with leading zeroes. That tells PHP that the value is Octal, not Decimal. Try running any PHP code using the "numbers" 08 or 09! https://www.php.net/manual/en/language.types.integer.php "To use octal notation, precede the number with a 0 (zero). As of PHP 8.1.0, octal notation can also be preceded with 0o or 0O." Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/326828-using-php-to-display-different-images-based-on-the-date/#findComment-1650209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.