August 16, 200917 yr For one of my projects, I was told to have a different banner load based on the month of the year. So what I did was this: $today=date(l); // Find what today is? using date function //Monthly definitions if($month==January){ echo "<img src='images/january.png' /><br />January"; } else($month==February){ echo "<img src='images/february.png' /><br />February"; } Before I submit any of my projects, I have someone review it for me. So for this project, I had a friend of mine look it over, and he said it was improper method to accomplish the job. If that's improper, I am curious to find out what the proper method is. Can anyone help?
August 16, 200917 yr lower case L in the format argument for date gives you a textual representation of the day. Upper case F gives the month. I'm not sure if this method will always return an English string - could be a locale setting. Using the numeric format is safer - that's lower case M - gives you a values from 01-12. And your code doesn't seem to have quoted the string values you deal with. You also assign the return value to the variable $today - but you compare later on to a variable $month that doesn't seem set. $month=date('m'); // Find what today is? using date function //Monthly definitions if($month=='01'){ echo "<img src='images/january.png' /><br />January"; } else($month=='02'){ echo "<img src='images/february.png' /><br />February"; } Not sure what your friend means by improper - you'd have to ask him about that. But it doesn't look like you tested your code.
August 16, 200917 yr A sidenote - the <br/> is that really required? Is that placed there for layout purposes?
August 16, 200917 yr Or something like this?: $monthnumber = date('m'); $monthname = date("F", mktime(0, 0, 0, $monthnumber, 10)); echo '<img src="images/'.strtolower($monthname).'.png" />'.$monthname; That should do it!
August 16, 200917 yr Assuming it will always return English, that would work. But do you have to create a new time object? $monthname = date("F"); echo '<img src="images/'.strtolower($monthname).'.png" />'.$monthname;
August 16, 200917 yr Good point... I thought using the mktime would be a way to ensure it was in 'English' format - but I'm not anywhere near enough of a PHP expert to know such things! Assuming settings will return English names then your 2-liner version will work nicely.
August 16, 200917 yr That'd be easier! Or, if you wanted nice names, put the names of the months into an array and just use the month number to return the right one.
August 16, 200917 yr Author Oh oops. I honestly don't know what my friend was talking about. He said that I wasn't using a valid call when the script tells the server to get the current date. Well I'm glad to know, I didn't completely screw up my code. Strangely the "l" value does work for me. Or at least I think it did. In anycase, I'll just use the numeric method you guys/girls pointed out. Thanks everyone!
Create an account or sign in to comment